monica-application-unit-testing
Skill18 starsChanged 7 months agoMITView on GitHub
Apply Monica application testing rules when naming or migrating test projects, creating or reviewing behavior tests, host scenarios, persistence isolation, and provider seams. Load monica-application and the repository's project specification first.
In this repository34 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- monica-application-building-block-developmentSkill
- monica-application-microserviceSkill
- monica-application-modular-monolithSkill
- monica-applicationSkill
- monica-application-ui-developmentSkill
- monica-application-uiSkill
- monica-architectureSkill
- monica-coding-standardsSkill
- monica-contributionSkill
- monica-developmentSkill
- monica-docs-authoringSkill
- monica-frameworkSkill
- monica-guideSkill
- monica-infra-aiSkill
- monica-infra-configurationSkill
- monica-infra-hostingSkill
- monica-infra-jobsSkill
- monica-infra-messagingSkill
- monica-infra-observabilitySkill
- monica-infra-persistenceSkill
- monica-infra-uiSkill
- monica-infra-webSkill
- monica-opentelemetrySkill
- monica-requirement-designSkill
- monica-third-party-module-developmentSkill
- monica-troubleshootingSkill
- monica-ui-auditSkill
- monica-ui-bridge-debugSkill
- monica-ui-designSkill
- monica-ui-developmentSkill
- monica-ui-localizationSkill
- monica-unit-testingSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
15 rules. About 0 are common, 10 are distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈10
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 10
Test business behavior through a complete Monica host when composition matters.
- Distinctive: line 16
Search the repository's shared test layer (for example Platform.Testing or PlatformTestApplicationFactory, including an existing library …
- Distinctive: line 27
Call CreateAsync for each scenario.
- Very specific: line 34
Use $monica-infra-persistence for the current repository and UnitOfWork contracts.
- Distinctive: line 50
Do not initialize contexts manually, clear trackers to make writes pass, or reconstruct transaction behavior in test helpers.
- Distinctive: line 58
Default seams retain the real audit policy.
- Very specific: line 64
Call application.DrainOutboxAsync<TContext> explicitly to test delivery.
- Distinctive: line 66
Do not restore pre-commit transport publishing or sleeps to wait for an uncontrolled dispatcher.
- Distinctive: line 74
Keep Builders, TestDoubles, TestData and Factories test-only.
- Distinctive: line 75
Do not cache an application/provider on a reusable factory.
- Distinctive: line 77
Never share a MonicaApplication across root providers.
- Very specific: line 83
Add [Trait("BuildingBlock", "<project-relative-path>::<runtime-key>")] using the project-qualified catalog reference when …
- Distinctive: line 89
Use raw BuildingBlockFixture<TBuildingBlock> only when collaborators are explicit and composition, discovery, proxies, transactions, hosted …
- Very specific: line 95
Read references/standards.md for layout and ownership, references/templates.md for exact API examples, and references/database-isolation.md …
- Distinctive: line 97
Run the relevant test project using the repository's verification policy and paths understood by the selected SDK.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Monica Application Unit Testing
Load $monica-application and the repository-designated project specification before test design or edits, including direct invocation of this skill. Apply any adopted local test convention and use the repository's verification instructions. The shared specification defines rule strength; use specification evolution before introducing a new testing or execution model.
Test business behavior through a complete Monica host when composition matters. Each scenario owns its host and database; registrations are finalized before build. Arrange, act and assert use independent scopes.
For domain, role, shared-library, host, and test-support naming or migration, read test project ownership and naming.
Repository Test Infrastructure First
Search the repository's shared test layer (for example Platform.Testing or PlatformTestApplicationFactory, including an existing library awaiting rename) before writing a factory. Extend existing project infrastructure when it covers the scenario. Do not introduce another framework with different transaction semantics.
Workflow
- Inspect production startup, module registration, discovery assemblies, contexts, external adapters and existing test factories.
- Select the behavior's production owner and use its runnable test project according to the naming reference.
- Derive a stateless project recipe from MonicaTestApplicationFactory
<TDiscoveryAnchor>:- ConfigureMonica composes the relevant production module graph.
- TypeDiscoveryAssemblies includes all production assemblies needed by the scenario.
- ConfigureHost supplies host/environment inputs.
- ConfigureServices applies stable boundary replacements before build; call its base implementation.
- Call CreateAsync for each scenario. Its optional ISeamReplacementBuilder callback applies scenario-specific replacements before build.
- Arrange with application.SeedAsync<TContext,TResult>, execute with application.ExecuteAsync, and verify with application.VerifyAsync
<TContext>. - Assert business outcomes, persisted state and durable notifications separately from delivery.
- Dispose the application and run the target test project.
Write and Read Boundaries
Use $monica-infra-persistence for the current repository and UnitOfWork contracts; this section identifies the test boundary those contracts require.
- IRepository
<TEntity>exposes Add/Remove; keyed repositories load tracked aggregates with FindAsync/GetAsync. Mutate the loaded instance. No detached Update or repository save is required. - application.ExecuteAsync creates a fresh scope and calls the production IExecutionPipeline. Resolve the handler inside its callback. Verify returned failures and handled exceptions separately from explicit abort or an exception escaping the owner, according to the transaction contract. An early FlushAsync or direct context save stays inside that transaction.
- AddRepositoryDbContext defaults to UnitOfWork participation and enables the module. Independently managed infrastructure stores explicitly use Default. Multiple business contexts require explicit operation selection.
- Completed operation scopes are terminal. Each retry or independent operation gets a fresh scope. Catching an explicit abort does not recover the transaction; a returned business failure or a handled nested exception does not abort it.
- For mediator-specific behavior, resolve IMediator in a fresh scope and Send the actual request. IQuery
<T>and IQuery requests explicitly declare read-only intent regardless of HTTP method or name: they do not start a transaction by default, but join an existing Shared transaction. Test explicit consistency transactions separately; Independent creates a fresh scope without forcing a Query transaction. - For direct read-only calls, use application.CreateScope and resolve the service there. Direct resolution alone does not invoke the pipeline.
- Native EF queries stay in infrastructure; business query interfaces return materialized projections. Do not enumerate an IQueryable after its scope is disposed.
Arrange and Verify
application.SeedAsync<TContext,TResult> takes a typed context callback. Add a heterogeneous graph together, call db.SaveChangesAsync inside the callback, and return keys or DTOs. An unsaved callback fails visibly.
Each seed callback and VerifyAsync callback uses a fresh scope. Data remains in the scenario database across these scopes. Pass foreign keys or load existing parents when arranging later data; do not pass detached seed graphs into the act phase.
Do not initialize contexts manually, clear trackers to make writes pass, or reconstruct transaction behavior in test helpers.
Database and Audit
UseTestDatabase<TContext>() overrides provider options while preserving production context/provider registrations. It requires production AddRepositoryDbContext first. One named SQLite memory database is owned by each scenario/context type, with independent scoped connections. Optional configureOptions supplies necessary EF interceptors/options.
UseRealTestDatabase<TContext> for production-provider SQL, isolation and sharding tests. The caller owns schema/data isolation and cleanup. SQLite does not establish those guarantees.
Default seams retain the real audit policy. Replace ICurrentUser, TimeProvider and ID generators when deterministic inputs are needed; do not replace audit logic with a no-op.
Event Assertions
- Explicit domain effects use IDomainEventQueue and same-scope IDomainEventHandler handlers before commit.
- Committed notifications/integration events use ordinary
[Outbox]EventBus payloads. Inspect persisted JSON snapshots before delivery. - Call application.DrainOutboxAsync
<TContext>explicitly to test delivery. Keep the scoped bus gateway and replaceIEventTransportwith a recording double. - Delivery is at least once. Exercise a failure after receipt but before acknowledgment and assert stable message identity and optional
[Inbox]consumer deduplication. - Do not restore pre-commit transport publishing or sleeps to wait for an uncontrolled dispatcher.
Ownership and Naming
- Project identity and cross-project scenario placement follow the naming reference.
- Factory: {Boundary}TestApplicationFactory; field: _factory.
- Classes: {TypeUnderTest}Tests; methods: Method_WhenCondition_ShouldExpectation.
- Mirror production folders such as HandlersCommand, HandlersQuery, DomainServices, Repositories, Entities and Modules.
- Keep Builders, TestDoubles, TestData and Factories test-only.
- Do not cache an application/provider on a reusable factory.
- A built service provider is immutable. CreateScope never replaces registrations.
- Never share a MonicaApplication across root providers.
- Independent scenarios run in parallel. Serialize only a named real external resource that cannot be isolated.
Requirement and Unit Traits
- Declare the governing requirement once per test class with
[Trait("REQ", "<requirement-id>")]; add a method-level REQ trait only when one test exercises an additional requirement. - Add
[Trait("BuildingBlock", "<project-relative-path>::<runtime-key>")]using the project-qualified catalog reference when{TypeUnderTest}Testsis absent or ambiguous. Class traits apply to every method; method traits add associations only for that method. Explicit Unit traits take precedence over naming inference. REQandUnitdeclare separate relationships. A unit association does not imply verification of every requirement on that unit.dotnet test --filter "REQ=<requirement-id>"selects declared tests; verify the selected count and execution result before claiming coverage. Runtime result formats may expose traits for CI cross-checks.- Trait arguments must be constant, non-empty strings. Requirement IDs must resolve to current Knowledge; use the allocated opaque identity, not a historical title or rewritten ID. Other trait keys stay free for repository-local tooling.
Smaller Boundaries
Use raw BuildingBlockFixture<TBuildingBlock> only when collaborators are explicit and composition, discovery, proxies, transactions, hosted lifecycle and host ownership are outside the assertion. It does not supply a substitute production UoW. Entity/value-object invariant tests may construct objects directly.
Replace external boundaries, not domain services, repositories or policy logic. Avoid uncontrolled network/database access and hidden machine dependencies. Keep assertions in tests.
References and Validation
Read references/standards.md for layout and ownership, references/templates.md for exact API examples, and references/database-isolation.md for provider choices.
Run the relevant test project using the repository's verification policy and paths understood by the selected SDK. Keep build/test processes sharing outputs sequential and resolve all build warnings.
For adopted application UI, verify sequential and concurrent operation scopes, current circuit identity (including explicit anonymous callers), nested mediator scope preservation, validation/authorization parity with HTTP, rollback and cancellation through a real host. Use browser smoke checks for layout, themes and route refresh; UI rendering tests remain opt-in.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

