test: add Basket aggregate unit tests#163
Merged
Merged
Conversation
Adds BasketTests.cs under Contoso.Shopping.Test.Unit/Domains, exercising the Basket aggregate directly (no repository, no Application service) as a real example of the coreex-aggregate skill's unit-testing guidance: aggregates have no injected dependencies (beyond reference data) and are the best unit-test target for both happy-path and guard-rejection logic. - Apply_Discount_To_Existing_Active_Basket: happy path, verifies DiscountAmount/Total after ApplyDiscount on an Active basket - Apply_Discount_To_Existing_CheckedOut_Basket: guard-rejection path, verifies OnCheckCanMutate's BusinessException on a CheckedOut basket and that state is left unchanged Wiring: - Contoso.Shopping.Test.Unit.csproj: add ProjectReference to Contoso.Shopping.Domain so the Domain namespace is reachable - EntryPoint.cs: register BasketStatus reference data in the in-memory ReferenceDataServiceDecorator (Basket.Status setter requires active ref-data via ThrowIfInactive()) Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds aggregate-focused unit tests for the Shopping Basket domain model, plus the minimal wiring needed for reference-data-backed state transitions to work in the unit-test host.
Changes:
- Added
BasketTestscoveringApplyDiscounthappy-path behavior and checked-out guard rejection. - Registered
BasketStatusreference data in the unit-testReferenceDataServiceDecoratorto satisfyThrowIfInactive()checks. - Added a project reference to
Contoso.Shopping.Domainfrom the unit test project.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| samples/tests/Contoso.Shopping.Test.Unit/EntryPoint.cs | Adds BasketStatus to the in-memory reference data decorator so Basket.Status can be set/validated in tests. |
| samples/tests/Contoso.Shopping.Test.Unit/Domains/BasketTests.cs | New unit tests exercising the Basket aggregate directly (discount apply + mutation guard failure). |
| samples/tests/Contoso.Shopping.Test.Unit/Contoso.Shopping.Test.Unit.csproj | References the Domain project so the aggregate types are directly accessible from unit tests. |
- BasketTests.cs: renamed Apply_Discount_To_Existing_Active_Basket /
Apply_Discount_To_Existing_CheckedOut_Basket to
Basket_ApplyDiscount_Success / Basket_ApplyDiscount_Invalid_Status,
matching the {Entity}_{Action}_{Outcome} convention documented in
coreex-tests.instructions.md (e.g. Basket_Checkout_Success).
- coreex-aggregate workflow.md: updated the Unit Testing Aggregates
example test names from {MutationMethod}_Succeeds_When_{Condition} /
{MutationMethod}_Fails_When_{GuardCondition} to
{Aggregate}_{MutationMethod}_Success / {Aggregate}_{MutationMethod}_{GuardOutcome},
and added an explicit callout of the naming convention with a
cross-reference to the existing Basket_Checkout_* examples.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
BasketTests.csunderContoso.Shopping.Test.Unit/Domains/, exercising theBasketaggregate directly — no repository, no Application service. This is a real, working example of the guidance added to thecoreex-aggregateskill (#162): aggregates have no injected dependencies (beyond reference data), making them the best unit-test target in the codebase for both happy-path and business-rule-rejection coverage.What's included
Apply_Discount_To_Existing_Active_Basket— happy path: verifiesDiscountAmount/TotalafterApplyDiscounton anActivebasketApply_Discount_To_Existing_CheckedOut_Basket— guard-rejection path: verifiesOnCheckCanMutate()'sBusinessExceptionon aCheckedOutbasket, and that state is left unchangedWiring changes
Contoso.Shopping.Test.Unit.csproj— addedProjectReferencetoContoso.Shopping.Domainso theDomainnamespace is reachable (resolves via enclosing-namespace lookup since the test class lives inContoso.Shopping.Test.Unit.Domains, a sibling ofContoso.Shopping.Domain)EntryPoint.cs— registeredBasketStatusreference data in the in-memoryReferenceDataServiceDecorator, sinceBasket.Status's setter requires active ref-data viaThrowIfInactive()Validation
dotnet build samples/tests/Contoso.Shopping.Test.Unit/Contoso.Shopping.Test.Unit.csproj— 0 warnings, 0 errorsdotnet test— all 16 tests pass across net8.0/net9.0/net10.0 (2 new + 14 pre-existing)Co-authored-by: Copilot App 223556219+Copilot@users.noreply.github.com