Write xUnit tests with Moq and FluentAssertions
✓Works with OpenClaudeYou are a .NET testing expert. The user wants to write xUnit tests using Moq for mocking and FluentAssertions for fluent assertions.
What to check first
- Run
dotnet --versionto confirm .NET SDK is installed - Verify xUnit, Moq, and FluentAssertions NuGet packages are in your
.csproj:dotnet list package - Confirm your test class inherits from or uses
[Fact]or[Theory]attributes from xUnit
Steps
- Add NuGet packages:
dotnet add package xunit && dotnet add package xunit.runner.visualstudio && dotnet add package Moq && dotnet add package FluentAssertions - Create a test class ending in
TestsorTest(xUnit convention) and addusing Xunit; - Mark each test method with
[Fact]for single-scenario tests or[Theory]with[InlineData()]for parameterized tests - Create Moq mocks with
new Mock<IYourInterface>()and set up method returns using.Setup(m => m.Method()).Returns(value) - Instantiate the class under test (SUT), injecting mocked dependencies via constructor
- Call the method being tested on the SUT and capture the result
- Use FluentAssertions methods like
.Should().Be(),.Should().Contain(),.Should().Throw<>()instead of Assert statements - Verify mock calls with
.Verify(m => m.Method(), Times.Once())if needed
Code
using Xunit;
using Moq;
using FluentAssertions;
using YourNamespace.Services;
using YourNamespace.Models;
public class OrderServiceTests
{
[Fact]
public void CalculateTotal_WithValidItems_ReturnsCorrectSum()
{
// Arrange
var mockRepository = new Mock<IOrderRepository>();
var order = new Order { Id = 1, Items = new List<OrderItem>
{
new OrderItem { Price = 10m, Quantity = 2 },
new OrderItem { Price = 5m, Quantity = 3 }
}};
mockRepository
.Setup(r => r.GetOrder(1))
.Returns(order);
var sut = new OrderService(mockRepository.Object);
// Act
var total = sut.CalculateTotal(1);
// Assert
total.Should().Be(35m);
mockRepository.Verify(r => r.GetOrder(1), Times.Once);
}
[Theory]
[InlineData(0, 0)]
[InlineData(100, 15)]
[InlineData(500, 50)]
public void ApplyDiscount_WithDifferent
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related .NET / C# Skills
Other Claude Code skills in the same category — free to download.
ASP.NET Core API
Scaffold ASP.NET Core Web API with controllers and middleware
Entity Framework Core
Set up Entity Framework Core with migrations and queries
ASP.NET Auth
Configure ASP.NET Identity with JWT and cookie authentication
Minimal API
Build minimal APIs with endpoint mapping
Blazor
Create Blazor components for interactive web UI
SignalR
Implement real-time communication with SignalR
.NET Background Services
Create background services with IHostedService
Want a .NET / C# skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.