The Standard Testing

Installation
SKILL.md

The Standard Testing

What this skill is

This skill governs how The Standard is tested, verified, and proven. It covers test-driven development, validation strategies, exception mapping, unit test structure, controller tests, and UI component tests.

Explicit coverage map

This skill explicitly covers:

  • Foundation-service implementation and validation/testing patterns from the Services chapter
  • Structural, logical, external, and dependency validation testing
  • Exception mapping and category testing
  • Processing-service testing responsibilities
  • Orchestration-service call-order testing
  • Aggregation-service testing restrictions
  • REST controller unit and acceptance tests
  • UI component testing for bases, core components, and pages
  • The supplied implementation specification sections on unit testing, partial test organization, conventions, AAA, and test order
  • TDD FAIL/PASS discipline relevant to test creation and implementation verification

When to use

Use this skill whenever writing, reviewing, expanding, fixing, or sequencing tests. Use it whenever deciding what to test first, how to map exceptions in tests, or how to prove a Standard-compliant flow.

Core testing doctrine

  1. Follow TDD.
  2. Write the failing test first.
  3. Verify the test actually fails.
  4. Write the minimum implementation required to pass.
  5. Verify the full relevant suite passes.
  6. Refactor without changing behavior.
  7. Repeat.

Validation testing rules

Validation Source of Truth

  1. Validation rules MUST be inferred from all authoritative sources:

    • Foundation service business rules
    • Storage-layer configuration (StorageBroker.[Entity].Configurations.cs)
    • Domain expectations implied by usage
  2. The storage configuration represents minimum enforced constraints:

    • Required vs optional
    • Maximum length
    • Minimum length (if configured)
    • Precision / scale / format where applicable
  3. Foundation services represent the enforcement boundary:

    • All constraints that can cause persistence failure MUST be validated before reaching storage
    • Validation must prevent database exceptions where deterministically possible

Validation Alignment Rules

  1. Foundation validation MUST be equal to or stricter than storage constraints.

  2. The following are ALLOWED (strengthening rules):

    • Storage: optional → Foundation: required
    • Storage: optional → Foundation: constrained (min/max length)
    • Storage: max length → Foundation: smaller max length
  3. The following are NOT ALLOWED (weakening or missing rules):

    • Storage: required → Foundation: not validated as required
    • Storage: max length → Foundation: no length validation
  4. Violations of alignment MUST be treated as:

    • A design defect
    • A test failure condition
    • A review blocker

Validation Responsibility Rule

  1. The database MUST NOT be relied upon to enforce:

    • Required field validation
    • Length validation
    • Format validation
  2. The ONLY acceptable database-enforced constraints without prior validation are:

    • Foreign key constraints
    • Uniqueness / duplicate key constraints
    • Concurrency constraints
  3. Any validation that can be performed deterministically in the foundation service MUST be performed there

Validation Test Derivation from Storage Configuration

  1. Storage configuration (StorageBroker.[Entity].Configurations.cs) defines constraints, not validation behavior.

  2. For every constraint defined in storage configuration, there MUST exist a corresponding validation rule in the foundation service:

    • Required fields
    • Length constraints (min/max)
    • Precision / format constraints where applicable
  3. Validation tests MUST:

    • Target the foundation service validation methods only
    • NOT directly test storage configuration behavior
    • Prove that each storage-defined constraint is enforced at the foundation layer
  4. For each property constraint, validation tests MUST focus on constraint violations and boundary breaches:

    • Invalid case (violates constraint) → FAIL

      • Required field missing / null
      • Value exceeding maximum length
      • Value below minimum length (if applicable)
    • Boundary violation cases:

      • Just above maximum → FAIL
      • Just below minimum → FAIL (if applicable)
  5. Valid scenarios (within acceptable range) are covered by logic tests and MUST NOT be redundantly tested in validation tests.

  6. Missing alignment between storage configuration and foundation validation MUST be treated as:

    • A design defect
    • A test failure condition
    • A review blocker
  7. Automation MAY assist in identifying constraints (e.g., via EF metadata), but:

    • Generated tests MUST still validate foundation behavior
    • Automation MUST NOT result in tests that validate the database layer directly
  8. All validation tests MUST:

    • Follow Standard naming conventions
    • Be explicit and intention-revealing

Validation order

  1. Structural validations first.
  2. Logical validations second.
  3. External validations third.
  4. Dependency validations when the external resource is the source of the failure.

Circuit-breaking validations

  1. Null checks and other hard-stop guards must break immediately.
  2. If continuing would create invalid dereference or meaningless work, stop immediately.

Continuous validations

  1. When multiple fields can be invalid independently, collect them before throwing.
  2. Use upsertable exception data.
  3. Use dynamic rules with condition + message.
  4. Use a validations collector routine.
  5. Throw once the collector contains errors.

Hybrid continuous validations

  1. Validate parent objects before validating child properties.
  2. Split nested validation into levels to avoid unintended null-reference failures.

Foundation-service test rules

  1. Test the happy path first.
  2. Then test structural validations.
  3. Then test logical validations.
  4. Then test external validations.
  5. Then test dependency validations.
  6. Then test dependency exceptions.
  7. Then test service exceptions.
  8. Always verify logging and broker calls.
  9. Always verify no unwanted calls occurred.
  10. Always keep validation and exception behaviors local and explicit.

Processing-service test rules

  1. Test higher-order logic, not primitive broker details.
  2. Validate only what the processing service uses.
  3. Test shifters.
    • Example: object -> bool or object -> count.
  4. Test combinations.
    • Example: retrieve + add, retrieve + modify, ensure-exists, upsert.
  5. Test processing exception mapping from foundation exceptions.

Orchestration-service test rules

  1. Test multi-entity flow combinations.
  2. Test mapping/branching between contracts when present.
  3. Test call order when the flow depends on order.
  4. Prefer natural order when inputs/outputs force sequencing.
  5. Use explicit order verification when sequencing is not naturally encoded.
  6. Verify orchestration-level exception wrapping and unwrapping.
  7. Test normalization outcomes indirectly through dependency shape and resulting behavior.

Aggregation-service test rules

  1. Do not test dependency call order in aggregation services.
  2. Do not use mock-sequence style order assertions for aggregation services.
  3. Test only basic structural validations and exposure-level aggregation behavior.
  4. Aggregation services may multi-call or pass-through; test the contract and exposure abstraction, not orchestration logic.

Controller and protocol test rules

  1. Controllers require unit tests for mapping logic.
  2. Unit-test success code mappings.
  3. Unit-test validation / dependency / service error mappings.
  4. Unit-test security i.e authorization / authentication failure mappings.
  5. Acceptance-test every endpoint.
  6. Clean up test data after acceptance tests.
  7. Emulate external resources not owned by the microservice when running acceptance tests.
  8. Integration and end-to-end testing are valid beyond unit + acceptance.

UI component test rules

Base components

  1. Treat bases as thin wrappers.
  2. Test their exposed APIs and wrapper behavior when needed.
  3. Do not put business logic into bases.

Core components

  1. Core components are test-driven.
  2. Test elements.
    • Existence
    • Properties
    • Actions
  3. Existence may be tested by property assignment, searching by id, or general search.
  4. Test styles when styles are part of the component contract.
  5. Test actions that mutate state, create components, or trigger service calls.
  6. Core components should integrate with one and only one view service.

Pages / containers

  1. Pages are simpler route containers.
  2. They generally do not require unit tests.
  3. They should not contain business logic.

Unit-test conventions from the supplied implementation profile

  1. Mirror partial-class split in tests.
  2. Use setup/helpers in the root test file.
  3. Split tests into logic, validations, and exceptions files.
  4. Use GWT: Given / When / Then.
  5. Mock all dependencies.
  6. Use readable assertions.
  7. Use deep cloning to protect expectation identity.
  8. Use randomized data by default.
  9. Verify exact dependency calls.
  10. End with VerifyNoOtherCalls.
  11. Keep naming explicit and scenario-driven.

Exact test implementation order for foundation-service add routines

When implementing an Add{Entity}Async routine under the implementation profile, follow this order:

  1. ShouldAdd{Entity}Async
  2. ShouldThrowValidationExceptionOnAddIf{Entity}IsNullAndLogItAsync
  3. ShouldThrowValidationExceptionOnAddIf{Entity}IsInvalidAndLogItAsync
  4. ShouldThrowDependencyValidationExceptionOnAddIfBadRequestErrorOccursAndLogItAsync
  5. ShouldThrowDependencyValidationExceptionOnAddIfConflictErrorOccursAndLogItAsync
  6. ShouldThrowCriticalDependencyExceptionOnAddIfUnauthorizedErrorOccursAndLogItAsync
  7. ShouldThrowCriticalDependencyExceptionOnAddIfForbiddenErrorOccursAndLogItAsync
  8. ShouldThrowCriticalDependencyExceptionOnAddIfNotFoundErrorOccursAndLogItAsync
  9. ShouldThrowCriticalDependencyExceptionOnAddIfUrlNotFoundErrorOccursAndLogItAsync
  10. ShouldThrowDependencyExceptionOnAddIfInternalServerErrorOccursAndLogItAsync
  11. ShouldThrowDependencyExceptionOnAddIfServiceUnavailableErrorOccursAndLogItAsync
  12. ShouldThrowCriticalDependencyExceptionOnAddIfHttpRequestErrorOccursAndLogItAsync
  13. ShouldThrowServiceExceptionOnAddIfServiceErrorOccursAndLogItAsync

For storage-based services, substitute the storage equivalents such as duplicate-key, DbUpdate, and SQL exceptions.

Testing and exception/localization addendum from the supplied implementation profile

8. Unit Testing

Unit tests follow The Standard's partial-class + three-axis approach.

8.1 Test Class Structure

Each entity's tests mirror the same partial-class split as the service:

Partial file Tests
{Entity}ServiceTests.cs Setup, mocks, helpers (CreateRandom{Entity}, etc.)
{Entity}ServiceTests.Logic.{Method}.cs Happy-path / success-case tests
{Entity}ServiceTests.Validations.{Method}.cs Validation failure tests
{Entity}ServiceTests.Exceptions.{Method}.cs Dependency & service exception tests

8.2 Conventions

Convention Detail
Mocking MoqMock<IStorageBroker>, Mock<IModernApiBroker>, Mock<ILoggingBroker>
Assertions FluentAssertionsShould().BeEquivalentTo()
Deep cloning DeepCloner — to isolate input/expected/actual objects
Data generation Tynamix.ObjectFillerFiller<{Entity}> with custom property setup
Exception comparison Xeption.SameExceptionAs() via SameExceptionAs expression helper
Test naming Should{Action}Async / ShouldThrow{Exception}On{Action}If{Condition}AndLogItAsync
Verify calls Every test verifies broker calls (Times.Once / Times.Never) and ends with VerifyNoOtherCalls()
Test framework xUnit[Fact] for single cases, [Theory] [InlineData] for parameterised cases

8.3 Test Pattern — GWT (Given / When / Then)

// given  — build input, configure mocks, construct expected exception
// when   — invoke the service method
// then   — assert result / exception, verify broker interactions

8.4 Test Implementation Order

Tests must be written and committed in the following strict order. This ordering ensures each category builds upon the prior one:

  1. Happy PathShouldAdd{Entity}Async
  2. Structural Validations — null entity check (ShouldThrowValidationExceptionOnAddIf{Entity}IsNullAndLogItAsync)
  3. Logical Validations — property-level checks using [Theory] [InlineData] (ShouldThrowValidationExceptionOnAddIf{Entity}IsInvalidAndLogItAsync)
  4. External Dependency Validation ExceptionsBadRequestConflict
  5. External Critical Dependency ExceptionsUnauthorizedForbiddenNotFoundUrlNotFound
  6. External Non-Critical Dependency ExceptionsInternalServerErrorServiceUnavailable
  7. Transport-Level ExceptionHttpRequestException
  8. Catch-All Service ExceptionException

For storage-based services, steps 4–7 are replaced with the corresponding SQL/EF exceptions (DuplicateKeyException, DbUpdateException, SqlException).

Rule — Test Verification Before Commit: Each FAIL commit must have the test actually running and failing. Each PASS commit must have all tests running and passing. Never commit a FAIL without verifying the test runner reports a genuine failure. See Section 12.1.3 — Commits for details.


9. Key Libraries

Package Purpose
Xeption Enhanced exceptions with data aggregation
EFxceptions EF Core wrapper that throws meaningful exceptions
RESTFulSense HTTP client wrapper for external API brokers
Moq Mock framework for unit tests
FluentAssertions Readable assertion syntax
DeepCloner Value-based deep cloning of test objects
Tynamix.ObjectFiller Random test data generation
xunit Unit test framework

Exception Handling Principles

0. Scope

These rules govern exception design, localisation, categorisation, propagation, and testing across all service layers.


1. Localisation (MANDATORY)

  1. External (non-local) exceptions MUST be localised at the boundary (Foundation).
  2. Native exceptions (SQL, HTTP, SDK) MUST NOT cross service boundaries.
  3. Localisation MUST convert native exceptions into domain-specific exceptions.

Data Preservation (MANDATORY)

  1. ALL relevant data from the external exception MUST be copied to the local exception:

    • Data dictionary
    • Constraint / validation metadata
    • Identifiers / keys
  2. The localised exception MUST carry this data so that:

    • The immediate inner exception contains full validation detail after categorisation.

2. Categorisation

  1. All exceptions MUST be categorised into one of:

    • Validation
    • DependencyValidation
    • Dependency
    • Service
  2. Categorisation defines upstream handling and exposer mapping.


3. Propagation (Unwrap / Rewrap)

Each service layer MUST:

  1. Catch downstream exceptions
  2. UNWRAP the categorical exception
  3. PRESERVE the LOCAL exception
  4. REWRAP into its OWN categorical exception

This prevents leakage of lower-layer concerns and enforces layer contracts.


4. Inner Exception Preservation (MANDATORY)

  1. The original local exception MUST always be preserved as the inner exception.
  2. No layer may discard or replace the local exception.

This guarantees:

  • Traceability
  • Correct exposer mapping (e.g. HTTP Conflict / FailedDependency)
  • Retention of validation data

5. Layer Responsibilities

Foundation

  1. Localise external exceptions
  2. Populate local exception data
  3. Categorise into Validation / DependencyValidation / Dependency / Service

Processing

  1. MUST ONLY handle categorised exceptions
  2. MUST NOT depend on foundation exception types
  3. MUST rewrap into processing-level exceptions

Orchestration

  1. MUST handle exceptions from all dependencies
  2. MUST unify into a single categorical exception per type
  3. MUST unwrap and preserve inner exceptions

6. Catch-All (MANDATORY)

  1. Every service MUST implement:
    • catch (Exception)
  2. MUST map to ServiceException

This prevents leakage of unknown failures.


7. Logging

  1. Each layer MUST log BEFORE rethrowing
  2. MUST log the categorised exception only

8. Testing

Orchestration Exception Tests

  1. SHOULD use [Theory]
  2. MUST cover multiple dependency exception types in a single test
  3. MUST avoid duplication

9. Design Intent

These rules ensure:

  1. Full abstraction from external systems
  2. Stable layer contracts
  3. Simplified exposer logic
  4. Complete validation visibility at the local exception level

Exception Handling Cross-References (Enforcement)

Validation Testing Alignment

  1. All validation tests MUST align with Exception Handling Principles:

    • Localisation MUST be verified (no native exceptions exposed)
    • Data preservation MUST be verified on local exceptions
    • Validation exceptions MUST contain full error details in inner exception
    • From processing service layer upwards, validation exceptions and dependency validation exceptions from its dependencies rewrap to [Entity][Layer]DependencyValidationExceptions
    • From processing service layer upwards, dependency exceptions and service exceptions from its dependencies rewrap to [Entity][Layer]DependencyExceptions
  2. Validation tests MUST assert:

    • Correct local exception type
    • Correct categorised exception type
    • Inner exception contains validation data (Data dictionary populated)

Foundation Exception Tests (Enforcement)

  1. Tests MUST verify localisation:

    • Native exception → Local exception → Categorised exception
  2. Tests MUST verify:

    • External exception data is copied to local exception
    • Local exception is preserved as inner exception
  3. Tests MUST NOT allow:

    • Native exception leakage
    • Missing Data dictionary propagation

Processing Exception Tests (Enforcement)

  1. Tests MUST assert:

    • Rewrapping into processing-level exception
    • Inner exception preservation
  2. Tests SHOULD:

    • Use [Theory] to test multiple dependency validation exceptions of the same type in one test
    • Use [Theory] to test multiple dependency exceptions of the same type in one test
    • Avoid duplication by testing multiple cases in a single test method

Orchestration Exception Tests (Enforcement)

  1. Tests MUST assert:

    • Rewrapping into orchestration-level exception
    • Inner exception is preserved (local exception)
    • Categorical exception is replaced at orchestration level
  2. Tests SHOULD:

    • Use [Theory] to test multiple dependency validation exceptions of the same type in one test
    • Use [Theory] to test multiple dependency exceptions of the same type in one test
    • Avoid duplication by testing multiple cases in a single test method

Catch-All Enforcement

  1. Tests MUST verify:
    • Unknown exceptions are mapped to ServiceException
    • No raw Exception escapes any service layer

Logging Enforcement

  1. Tests MUST verify:
    • Logging occurs before exception is thrown
    • Logged exception is the categorised exception

Design Integrity Rule

  1. Any violation of exception handling principles MUST be treated as:
    • A design defect
    • A test failure
    • A review blocker
Weekly Installs
GitHub Stars
16
First Seen