Skip to content

test: add Basket aggregate unit tests#163

Merged
chullybun merged 2 commits into
mainfrom
tests/basket-aggregate-unit-tests
Jul 1, 2026
Merged

test: add Basket aggregate unit tests#163
chullybun merged 2 commits into
mainfrom
tests/basket-aggregate-unit-tests

Conversation

@chullybun

Copy link
Copy Markdown
Collaborator

Summary

Adds BasketTests.cs under Contoso.Shopping.Test.Unit/Domains/, exercising the Basket aggregate directly — no repository, no Application service. This is a real, working example of the guidance added to the coreex-aggregate skill (#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: 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 changes

  • Contoso.Shopping.Test.Unit.csproj — added ProjectReference to Contoso.Shopping.Domain so the Domain namespace is reachable (resolves via enclosing-namespace lookup since the test class lives in Contoso.Shopping.Test.Unit.Domains, a sibling of Contoso.Shopping.Domain)
  • EntryPoint.cs — registered BasketStatus reference data in the in-memory ReferenceDataServiceDecorator, since Basket.Status's setter requires active ref-data via ThrowIfInactive()

Validation

  • dotnet build samples/tests/Contoso.Shopping.Test.Unit/Contoso.Shopping.Test.Unit.csproj — 0 warnings, 0 errors
  • dotnet 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

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>
Copilot AI review requested due to automatic review settings July 1, 2026 22:10
@chullybun chullybun added this to the v4.0.0-preview-2 milestone Jul 1, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 BasketTests covering ApplyDiscount happy-path behavior and checked-out guard rejection.
  • Registered BasketStatus reference data in the unit-test ReferenceDataServiceDecorator to satisfy ThrowIfInactive() checks.
  • Added a project reference to Contoso.Shopping.Domain from 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.

Comment thread samples/tests/Contoso.Shopping.Test.Unit/Domains/BasketTests.cs Outdated
Comment thread samples/tests/Contoso.Shopping.Test.Unit/Domains/BasketTests.cs Outdated
- 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>
@chullybun chullybun merged commit 3bc6c8c into main Jul 1, 2026
4 of 5 checks passed
@chullybun chullybun deleted the tests/basket-aggregate-unit-tests branch July 1, 2026 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants