agentleFS
LeanMeanCleanAppsLeanMeanCleanApps/CleanModelContextProtocol/.agents/skills/unit-tests/SKILL.md

unit-tests

Skill1 starsChanged 6 days agoMITView on GitHub

How unit tests are written with xUnit, Shouldly and NSubstitute - folder layout, naming, Arrange/Act/Assert, testing validators and handlers, and asserting the cancellation token. Use when testing a handler, validator, endpoint or any class in isolation.

16 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use auto-perf-optimize, chat-customizations-editor and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • MicrosoftSkill

    auto-perf-optimize

    Run agent-driven VS Code performance or memory investigations. Use when asked to launch Code OSS, automate a VS Code scenario, run the Chat memory smoke runner, capture renderer heap snapshots, take workflow screenshots, compare run summaries, or drive a repeatable scenario before heap-snapshot analysis.

    Also in 5 projects that use this

  • MicrosoftSkill

    chat-customizations-editor

    Use when working on the Chat Customizations editor — the management UI for agents, skills, instructions, hooks, prompts, MCP servers, and plugins.

    Also in 5 projects that use this

  • OpenCortexIDESkill

    add-policy

    Use when adding, modifying, or reviewing VS Code configuration policies. Covers the full policy lifecycle from registration to export to platform-specific artifacts. Run on ANY change that adds a `policy:` field to a configuration property.

    Also in 4 projects that use this

  • +9 more from microsoft

The file

We split this skill into the rules it gives an agent

6 rules. 0 are common, 3 are distinctive, and 3 are very specific to this project.

  • 0

    Common

    Most of the 15 edited versions we read keep these.

  • 3

    Distinctive

    This author's own choices. Worth a look.

  • 3

    Very specific

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

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

    Create <Class>Tests.cs in the matching folder, for example …

    line 25
  2. Very specific:

    Use a real CancellationTokenSource so you can prove the token was passed on (cancellation).

    line 29
  3. Very specific:

    Use Assert.Equal or FluentAssertions.

    line 74
  4. Distinctive:

    Use Moq. NSubstitute only.

    line 75
  5. Distinctive:

    Test private methods, or test through the database in a unit test.

    line 76
  6. Distinctive:

    Write a test that only exists to raise coverage without asserting behaviour.

    line 78

“Common” is measured: kept by at least 6 in 10 of the 15 edited versions of this file we could read. “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

Unit tests

The rule

xUnit for tests, Shouldly for assertions, NSubstitute for fakes. Test folders mirror the source folders. test-design says which tests a change needs; this skill says how to write them. Each test has // Arrange, // Act and // Assert comments and tests one thing.

ProjectTests
tests/Application.Tests.UnitHandlers (repositories faked with NSubstitute) and validators (FluentValidation.TestHelper)
tests/Presentation.Tests.UnitPresentation classes we wrote, called directly, with ISender faked where they send requests. Every template has this project; what goes in it depends on what its presentation layer holds

Test the code we wrote, not the libraries under it. A unit test that only proves ASP.NET Core, Hot Chocolate, Mapperly or the AWS serializer does its job protects nothing of ours. Test our settings, error handlers and filters, transformers, option extensions and parsing, and leave the libraries' own behaviour to the libraries.

Names read as Method_Should<Outcome>_<Condition>: Handle_ShouldThrowException_ReviewDoesNotExist, GetReviews_ShouldReturn_Ok, Validator_ShouldHaveValidationErrorFor_Id.

Steps

  1. Create <Class>Tests.cs in the matching folder, for example tests/Application.Tests.Unit/Reviews/Commands/UpdateReview/UpdateReviewHandlerTests.cs.
  2. Handlers: one test for the happy path, one per failure (not found, and so on). Fake each repository with Substitute.For<I...Repository>() and set returns with ReturnsForAnyArgs or Returns on specific arguments.
  3. Assert the result, then assert the calls: Received(1) with the exact arguments and token, and DidNotReceiveWithAnyArgs() for calls that mustn't happen.
  4. Validators: a static validator instance, one valid case with ShouldNotHaveAnyValidationErrors(), and a [Theory] per rule with ShouldHaveValidationErrorFor(...).
  5. Use a real CancellationTokenSource so you can prove the token was passed on (cancellation).
  6. To check a log message, pass a FakeLogger<T> (Microsoft.Extensions.Diagnostics.Testing) and assert on logger.Collector: the count, then the level, message and exception of LatestRecord.

Example

// tests/Application.Tests.Unit/Reviews/Commands/UpdateReview/UpdateReviewHandlerTests.cs
[Fact]
public async Task Handle_ShouldReturnUpdatedReview_ReviewExists()
{
    // Arrange
    var command = new UpdateReviewCommand
    {
        Id = Guid.NewGuid(),
        Stars = 1
    };

    var review = new Review(command.Id, command.Stars);
    var reviewsRepository = Substitute.For<IReviewsRepository>();

    _ = reviewsRepository.UpdateReview(default, default, default).ReturnsForAnyArgs(review);

    var handler = new UpdateReviewHandler(reviewsRepository);
    using var tokenSource = new CancellationTokenSource();
    var token = tokenSource.Token;

    // Act
    var result = await handler.Handle(command, token);

    // Assert
    result.ShouldBe(review);

    _ = await reviewsRepository.Received(1).UpdateReview(command.Id, command.Stars, token);
}
// tests/Application.Tests.Unit/Reviews/Commands/UpdateReview/UpdateReviewValidatorTests.cs
var result = Validator.TestValidate(command);

_ = result.ShouldHaveValidationErrorFor(command => command.Id);

Don't

  • Use Assert.Equal or FluentAssertions. Shouldly only.
  • Use Moq. NSubstitute only.
  • Test private methods, or test through the database in a unit test.
  • Share mutable state between tests through static fields.
  • Write a test that only exists to raise coverage without asserting behaviour.
  • Re-test a library's behaviour, such as a framework's model binding or a serializer's defaults.

Check

dotnet test tests/Application.Tests.Unit
dotnet test tests/Presentation.Tests.Unit

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.