mit einem Klick
tsp-csharp-xunit
// Comprehensive xUnit testing guide for C# projects. Use when writing unit tests, setting up test projects, creating data-driven tests, mocking dependencies, or organizing test suites.
// Comprehensive xUnit testing guide for C# projects. Use when writing unit tests, setting up test projects, creating data-driven tests, mocking dependencies, or organizing test suites.
| name | tsp-csharp-xunit |
| description | Comprehensive xUnit testing guide for C# projects. Use when writing unit tests, setting up test projects, creating data-driven tests, mocking dependencies, or organizing test suites. |
{ProjectName}.TestsMicrosoft.NET.Test.Sdk, xunit, xunit.runner.visualstudioFluentAssertions, NSubstitute (or Moq)dotnet test (use --filter for targeted runs)IDisposable for teardownIClassFixture<T> for shared context within a test classICollectionFixture<T> for shared context across test classesIAsyncLifetime for async setup/teardownpublic class OrderServiceTests : IClassFixture<DatabaseFixture>, IDisposable
{
private readonly DatabaseFixture _db;
public OrderServiceTests(DatabaseFixture db)
{
_db = db;
}
[Fact]
public void CreateOrder_WithValidItems_ReturnsOrderId()
{
// Arrange
var service = new OrderService(_db.Context);
var items = new[] { new OrderItem("SKU-1", 2) };
// Act
var result = service.CreateOrder(items);
// Assert
result.Should().BeGreaterThan(0);
}
public void Dispose() { /* cleanup */ }
}
{ClassUnderTest}Tests{Method}_{Scenario}_{ExpectedResult}GetUser_WhenIdIsInvalid_ReturnsNotFoundCalculateTotal_WithDiscount_AppliesPercentageSaveAsync_WhenCancelled_ThrowsOperationCancelled[Theory] + [InlineData] for simple inline values[Theory] + [MemberData] for method/property-based data[Theory] + [ClassData] for reusable data generators[Theory]
[InlineData(0, false)]
[InlineData(5, true)]
[InlineData(-1, false)]
public void IsValidQuantity_ReturnsExpected(int quantity, bool expected)
{
var result = OrderValidator.IsValidQuantity(quantity);
result.Should().Be(expected);
}
FluentAssertions when available:
result.Should().Be(expected)collection.Should().HaveCount(3).And.Contain(x => x.IsActive)act.Should().ThrowAsync<InvalidOperationException>()Assert.Equal, Assert.True, Assert.Throws<T>Assert.Contains / Assert.DoesNotContain for collectionsNSubstitute for readability; Moq is also acceptablevar repo = Substitute.For<IOrderRepository>();
repo.GetByIdAsync(42, Arg.Any<CancellationToken>())
.Returns(new Order { Id = 42 });
var service = new OrderService(repo);
var result = await service.GetOrderAsync(42, CancellationToken.None);
result.Should().NotBeNull();
result.Id.Should().Be(42);
[Trait("Category", "Integration")] for categorizationSkip = "reason" when conditionally disabledITestOutputHelper for diagnostic output (not Console.WriteLine)Thread.Sleep — use Task.Delay with cancellation tokens in async testsComprehensive C# and .NET development skill for TSP projects. Use when writing, reviewing, or debugging C# code — covers async/await, EF Core, performance, security, HTTP resilience, Options pattern, middleware, logging, and ASP.NET Core patterns. Trigger for: async, Task, CancellationToken, DbContext, LINQ, Span, Memory, hot path, authentication, authorization, validation, secrets, HttpClient, IOptions, middleware, minimal API, TypedResults, ProblemDetails, Channels, ILogger, Serilog, logging, pagination, entity design, audit fields, OpenAPI.
Use when: creating a release, publishing a version, cutting a release, tagging a release, releasing plugin, release workflow, prepare release, create git tag, create GitHub release, update version, bump version, and update changelog. Covers: changelog extraction/review, semantic version validation, version bumping in VS Code + Rider manifests, guarded git commit/tag flow, and release status messaging.