dotnet-testing-nsubstitute-mocking

Create and verify NSubstitute test doubles in .NET C# unit tests.

7|4|Updated Dec 12, 2023
One-click install
npx skills add https://github.com/BridgingIT-GmbH/bITdevKit --skill dotnet-testing-nsubstitute-mocking-bridgingit-gmbh
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-testing-nsubstitute-mocking
Source: https://github.com/BridgingIT-GmbH/bITdevKit/tree/main/.agents/skills/dotnet-testing-nsubstitute-mocking
Command: npx skills add https://github.com/BridgingIT-GmbH/bITdevKit --skill dotnet-testing-nsubstitute-mocking-bridgingit-gmbh

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps developers master creating and using test doubles (Dummy, Stub, Fake, Spy, Mock) with NSubstitute to isolate dependencies, verify interactions, and write reliable unit tests in C#.

Core Features & Use Cases

  • Comprehensive guidance on Substitute.For to create mocks and stubs, Returns to define outputs, Received to verify interactions, and Throws to simulate failures.
  • Real-world patterns: Dummy, Stub, Fake, Spy, and Mock with examples for DI, asynchronous flows, and error handling.
  • Use cases include validating method calls, argument matching, call order, and capturing arguments across typical testing scenarios.

Quick Start

Write and run a simple test that uses NSubstitute to substitute a dependency and verify a call is made.

Frequently Asked Questions about dotnet-testing-nsubstitute-mocking

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I mock dependencies in C# unit tests using NSubstitute?

To mock dependencies in C# unit tests using NSubstitute, use Substitute.For to create test doubles and Returns to define outputs. This isolates dependencies for reliable testing in .NET projects.

What is the difference between a stub and a mock in .NET dependency substitution?

In .NET dependency substitution, a stub provides predefined return values using Returns, while a mock verifies interactions using Received. This Skill covers both patterns for isolating dependencies.

Can I verify method calls and argument matching with NSubstitute in xUnit?

Yes, you can verify method calls and argument matching with NSubstitute in xUnit by using the Received method with argument matchers to validate interactions and capture arguments in C# tests.

How do I simulate exceptions for asynchronous flows in C# unit tests?

To simulate exceptions for asynchronous flows in C# unit tests, use the Throws method with NSubstitute. This allows testing error handling and failure scenarios in dependency substitution.

Does this NSubstitute guidance work with dependency injection and logging patterns?

Yes, this NSubstitute guidance works with dependency injection and logging patterns commonly used in .NET projects. It provides real-world testing scenarios for DI and asynchronous flows.

When should I use NSubstitute over other mocking frameworks for .NET unit testing?

Use NSubstitute for .NET unit testing when you need clear syntax for substituting dependencies, setting return values, and verifying calls. It handles complex argument matching and exception simulation effectively.