SSWConsulting / SSWConsulting/SSW.CleanArchitecture
✅ Consider encapsulating DbContext in integration tests
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 199
- Forks
- 37
- PR merge metrics
- No merged PRs in 30d
Description
Cc: @danielmackay
Hi @SSWConsulting/ssw-saw
Pain
Because the DbContext is scoped, the instance injected into the tests is different to the instance injected into the query handler. This can result in unexpected effects - as a scoped DbContext is a unit of work, saving changes in one instance will not reliably be reflected in other instances.
Suggested Solution
The DbContext could be encapsulated, with the test base exposing only an AddEntity method and a Query method.
Acceptance Criteria
Acceptance Criteria defines the requirements that must be met for the story to be completed. This is WHAT must be done.
See https://www.ssw.com.au/rules/acceptance-criteria.
- When I write unit tests and integration tests, existing test examples are clear and I can replicate them and get expected test outcomes
- Shared or common classes do not expose properties or methods that could allow me to write tests in a way that does not align with our intended best practice
- The testing apparatus guides me into the pit of success
Tasks
Tasks help developers to track small bits of work needed to meet the ACs. This is HOW the work will be done.
- [ ] Propose alternative solutions/approaches
- [ ] Discuss with SAW team
- [ ] (if applicable) implement proposed changes
Thanks!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the shared integration-test base, its DbContext injection, and existing test examples. Compare exposing only AddEntity and Query with current usage, then discuss alternative approaches with the SAW team. Done means test examples remain clear and predictable while common test classes prevent access patterns that bypass intended practices.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend, database, testing
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100