agentleFS
TairitsuaTairitsua/Monica/skills/monica-unit-testing/SKILL.md

monica-unit-testing

Skill18 starsChanged 7 months agoMITView on GitHub

Create, migrate, or review Monica framework tests and shared testing infrastructure, including host-owned scenarios, raw BuildingBlock fixtures, Roslyn source-generator tests, result assertions, and isolation.

The file

Show its rules and text

We split this skill into the rules it gives an agent

28 rules. About 0 are common, 10 are distinctive, and 18 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.

  • 18

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use Monica.Testing as the shared toolkit and keep runnable framework tests under tests/Test.Monica.*.

    line 8
  2. Very specific:

    Read <project-root>/tests/README.md for the current project layout and execution rules.

    line 12
  3. Very specific:

    Put reusable assertions, host helpers, and deterministic boundary doubles in Monica.Testing.

    line 19
  4. Distinctive:

    Prefer public-surface coverage: module registrations, facades, public models and abstractions, stable providers, and observable side …

    line 20
  5. Distinctive:

    Use the repository test runner or the relevant test project.

    line 21
  6. Very specific:

    Use MonicaTestApplicationFactory<TDiscoveryAnchor> when the test depends on real Monica composition.

    line 25
  7. Very specific:

    Use ConfigureHost(WebApplicationBuilder) for host configuration and environment inputs.

    line 28
  8. Very specific:

    Use ConfigureServices(IServiceCollection) for stable test-boundary registrations shared by every scenario produced by that factory.

    line 29
  9. Very specific:

    Call CreateAsync(...) for each test scenario.

    line 30
  10. Very specific:

    Call MonicaTestApplication.CreateScope(...) only to create a normal child scope.

    line 32
  11. Very specific:

    Do not copy service descriptors into another root provider, share one MonicaApplication across providers, or emulate per-scope registration …

    line 35
  12. Distinctive:

    Use it only when all collaborators are deliberately supplied and the assertion does not depend on:

    line 49
  13. Very specific:

    Use the host-owned scenario model for any of those behaviors.

    line 57
  14. Very specific:

    Test source generators with raw Roslyn inputs under a dedicated tests/Test.Monica.{GeneratorProject} project.

    line 61
  15. Very specific:

    Create all source text in memory, provide explicit metadata references, and run CSharpGeneratorDriver with the same language version the …

    line 64
  16. Distinctive:

    Run the same driver again with identical inputs and assert byte-identical generated sources and diagnostics.

    line 66
  17. Distinctive:

    Verify generator execution does not create or mutate files.

    line 67
  18. Very specific:

    When a published request uses a custom IRemoteResultEnvelope<TSelf>, test that CreateRemoteFailure returns a valid constructor-initialized …

    line 69
  19. Very specific:

    Test folders mirror source folders such as Modules/, Facades/, Providers/, Services/, Pages/, and Components/.

    line 77
  20. Distinctive:

    Test module behavior through registration extensions, dependencies, resulting options, or DI-visible registrations rather than static …

    line 82
  21. Distinctive:

    Avoid real network, uncontrolled persistence, sleeps, random inputs, and untracked machine state.

    line 84
  22. Very specific:

    Keep InternalsVisibleTo exceptional and scoped to the corresponding runnable test project.

    line 85
  23. Distinctive:

    Run independent test scenarios in parallel by default.

    line 86
  24. Very specific:

    Read references/standards.md for stable boundary and isolation rules.

    line 99
  25. Very specific:

    Read references/samples.md when selecting an existing framework sample.

    line 100
  26. Very specific:

    Use $monica-application-unit-testing for Monica-based business-service tests.

    line 101
  27. Distinctive:

    Keep build/test processes sharing outputs sequential.

    line 106
  28. Distinctive:

    Run the relevant runnable test project and the repository's default non-UI gate when required.

    line 107

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Monica Unit Testing

Use Monica.Testing as the shared toolkit and keep runnable framework tests under tests/Test.Monica.*. Choose the smallest test boundary that proves the behavior without misrepresenting Monica's activation or host lifecycle.

Workflow

  1. Read <project-root>/tests/README.md for the current project layout and execution rules.
  2. Classify the test before choosing infrastructure:
    • Pure logic: construct the value or service directly.
    • Raw Building Block collaboration: use BuildingBlockFixture<TBuildingBlock> and accept its activation limits.
    • Module wiring, options, conventional registration, proxies, hosted lifecycle, or cross-scope behavior: create a full host with MonicaTestApplicationFactory<TDiscoveryAnchor>.
    • Blazor behavior contract: use bUnit in the runnable UI test project only when the user explicitly requests UI testing or UI test additions.
    • Roslyn source-generator semantics: build an in-memory CSharpCompilation and run the generator through GeneratorDriver.
  3. Put reusable assertions, host helpers, and deterministic boundary doubles in Monica.Testing; keep scenario-specific data and doubles in the runnable test project.
  4. Prefer public-surface coverage: module registrations, facades, public models and abstractions, stable providers, and observable side effects.
  5. Use the repository test runner or the relevant test project. Keep processes that share build outputs sequential.

Host-Owned Scenarios

Use MonicaTestApplicationFactory<TDiscoveryAnchor> when the test depends on real Monica composition.

  • Derive a project factory and implement ConfigureMonica(IMonicaBuilder) with the production module graph needed by the scenario.
  • Use ConfigureHost(WebApplicationBuilder) for host configuration and environment inputs.
  • Use ConfigureServices(IServiceCollection) for stable test-boundary registrations shared by every scenario produced by that factory. Call the base implementation first unless the scenario intentionally replaces all standard seams.
  • Call CreateAsync(...) for each test scenario. Every call builds and starts a complete host with its own MonicaApplication, module graph, singleton services, logger ownership, and disposal boundary.
  • Pass scenario-specific seam replacements to CreateAsync(Action<ISeamReplacementBuilder>?). The callback changes registrations before the host is built.
  • Call MonicaTestApplication.CreateScope(...) only to create a normal child scope. A built service provider is immutable; scope creation never replaces registrations.
  • Dispose the MonicaTestApplication after the scenario, even when several scopes are used within that scenario.

Do not copy service descriptors into another root provider, share one MonicaApplication across providers, or emulate per-scope registration replacement.

Persistence Scenarios

Keep production context/provider registration. UseTestDatabase overrides options and keeps one database per scenario/context type, with fresh scoped connections. Arrange with application.SeedAsync<TContext,TResult> and an explicit save; act through application.ExecuteAsync and the real IExecutionPipeline; verify in application.VerifyAsync<TContext>. Do not add synthetic save loops or clear trackers to repair tests.

Default seams retain real audit policy; replace TimeProvider/current-user/ID inputs. SeedAsync suppresses entity projections. Test [Outbox] publishing through the real scoped EventBus gateway, inspect committed row snapshots separately from delivery, and explicitly call DrainOutboxAsync<TContext>. Replace IEventTransport, not IDistributedEventBus, for provider recording. Include rollback after an early flush for escaping exceptions and explicit abort, commit after returned failures or handled exceptions, generated-key capture, immutable payloads, retry identity, Inbox consumer uniqueness, and fresh-scope retries.

When a handler host has multiple write contexts, put [ExecutionTransaction(ExecutionTransactionMode.Automatic, DbContextTypes = new[] { typeof(PrimaryDbContext), typeof(ParticipantDbContext) })] on its concrete execution method and test the real receive pipeline. The selected primary context owns Inbox and Outbox rows; verify its final flush after later participants stage projections. Follow the persistence transaction contract for selection and explicit execution features.

Raw Building Block Fast Path

BuildingBlockFixture<TBuildingBlock> is an explicitly raw fast-path harness. It creates a small Microsoft DI container, activates the target, and initializes Monica's cached-service-provider accessor where applicable.

Use it only when all collaborators are deliberately supplied and the assertion does not depend on:

  • Monica module composition or options binding
  • conventional type discovery and registration
  • dynamic proxies or interceptors
  • hosted-service startup and shutdown
  • host-owned singleton or logging isolation

Use the host-owned scenario model for any of those behaviors. Do not introduce another application-service-specific fixture; ApplicationServiceFixture<THandler> is not part of the testing model.

Roslyn Generator Boundary

Test source generators with raw Roslyn inputs under a dedicated tests/Test.Monica.{GeneratorProject} project.

  • Reference the generator as a normal test dependency so the test can instantiate its public IIncrementalGenerator types. Do not rely on MSBuild analyzer execution for semantic generator tests.
  • Create all source text in memory, provide explicit metadata references, and run CSharpGeneratorDriver with the same language version the scenario requires.
  • Assert generator diagnostics, generated hint names and source, and the diagnostics of the updated output compilation.
  • Run the same driver again with identical inputs and assert byte-identical generated sources and diagnostics. This protects incremental determinism.
  • Verify generator execution does not create or mutate files. File export, package imports, and MSBuild target ordering belong in separate integration tests only when those build surfaces are intentionally under test.
  • For request-owned Web API generation, cover the public contract matrix: route, verb, binding, operation naming, cancellation, result shapes, published-API namespace selection, local-only HTTP exclusion, duplicate or invalid declarations, and same-compilation client/controller availability.
  • When a published request uses a custom IRemoteResultEnvelope<TSelf>, test that CreateRemoteFailure returns a valid constructor-initialized envelope and that cancellation is propagated rather than projected as a failure result.

Required Conventions

  • Shared toolkit: Monica.Testing/Monica.Testing.csproj
  • Runnable project: tests/Test.Monica.{ProjectName}/
  • Test class: {TypeName}Tests
  • Test method: Method_WhenCondition_ShouldExpectation
  • Test folders mirror source folders such as Modules/, Facades/, Providers/, Services/, Pages/, and Components/.

Monica-Specific Rules

  • Assert Result<T> explicitly. Facade tests cover Status, Message, and Data; successful string paths verify data and message separately. Cover Created as success, permitted null payloads, failure metadata forwarding, and meaningful failure reports. Use the Core runtime contracts and generator-project compilation probes for the explicit result API.
  • Test module behavior through registration extensions, dependencies, resulting options, or DI-visible registrations rather than static tables alone.
  • Supply doubles for external dependencies. Resolve application services, domain services, repositories, and options from the scenario host.
  • Avoid real network, uncontrolled persistence, sleeps, random inputs, and untracked machine state.
  • Keep InternalsVisibleTo exceptional and scoped to the corresponding runnable test project.
  • Run independent test scenarios in parallel by default. Serialize only tests that name and document a genuinely shared external resource.

Default Stack

  • xUnit v3
  • AwesomeAssertions
  • NSubstitute with its analyzer
  • coverlet.collector
  • bUnit in UI test projects only
  • Microsoft.CodeAnalysis.CSharp in source-generator test projects only

References

  • Read references/standards.md for stable boundary and isolation rules.
  • Read references/samples.md when selecting an existing framework sample.
  • Use $monica-application-unit-testing for Monica-based business-service tests.

Validation

  • Match path syntax to the SDK process actually running.
  • Keep build/test processes sharing outputs sequential.
  • Run the relevant runnable test project and the repository's default non-UI gate when required. UI projects run only on explicit user request.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.