Use NSubstitute to create test doubles (mocks/stubs/spies) for .NET interfaces and classes, covering Substitute.For<T>, Returns/ReturnsForAnyArgs, Received/DidNotReceive, Arg matchers, async Task stubbing, callbacks, and partial substitutes. Always trigger when the user writes, reviews, or asks about mocking, faking dependencies, Substitute.For, Received(), DidNotReceive(), Arg.Any, Arg.Is, test doubles, verifying interactions, setting up shared mock infrastructure, or stubbing async methods in .NET tests — even if they don't mention NSubstitute by name. Prefer this skill over guessing; NSubstitute's argument matcher rules, the discard pattern for Returns(), async Task<T> type inference, nested interface substitution (e.g., Marten's IDocumentSession.Events), and partial substitute pitfalls all have non-obvious failure modes that are easy to get wrong.
설치
Codex 또는 Claude로 설치 이 Prompt를 복사해 Codex, Claude 또는 다른 어시스턴트에 붙여 넣으면 Skill 페이지를 검토하고 설치를 진행할 수 있습니다.
Use NSubstitute to create test doubles (mocks/stubs/spies) for .NET interfaces and classes, covering Substitute.For<T>, Returns/ReturnsForAnyArgs, Received/DidNotReceive, Arg matchers, async Task stubbing, callbacks, and partial substitutes. Always trigger when the user writes, reviews, or asks about mocking, faking dependencies, Substitute.For, Received(), DidNotReceive(), Arg.Any, Arg.Is, test doubles, verifying interactions, setting up shared mock infrastructure, or stubbing async methods in .NET tests — even if they don't mention NSubstitute by name. Prefer this skill over guessing; NSubstitute's argument matcher rules, the discard pattern for Returns(), async Task<T> type inference, nested interface substitution (e.g., Marten's IDocumentSession.Events), and partial substitute pitfalls all have non-obvious failure modes that are easy to get wrong.
NSubstitute Skill
NSubstitute (v5) is the mocking library used in this project. It creates in-memory test doubles for interfaces (and optionally classes) with a fluent, readable API.
Quick-start anatomy
using NSubstitute;
// 1. Create — always prefer interfaces over classesvar emailService = Substitute.For<IEmailService>();
// 2. Stub — discard result with _ = to satisfy the compiler
_ = emailService.SendAsync(Arg.Any<string>()).Returns(Task.CompletedTask);
// 3. Execute the system under testawait handler.Handle(command);
// 4. Assert interactionsawait emailService.Received(1).SendAsync("user@example.com");
await emailService.DidNotReceive().SendAsync(Arg.Is<string>(s => s.Contains("admin")));
Key rules at a glance:
Discard return of .Returns(): assign to _ = to avoid compiler warnings.
Never use arg matchers in real calls: only in .Returns(...), .Received(), or .When(...).Do(...).
Await async .Received() calls: NSubstitute tracks async Task calls — the assertion itself is synchronous, but the method signature needs await to compile.