$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
.NET / C#intermediateNew

.NET Testing

Share

Write xUnit tests with Moq and FluentAssertions

Works with OpenClaude

You 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 --version to 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

  1. Add NuGet packages: dotnet add package xunit && dotnet add package xunit.runner.visualstudio && dotnet add package Moq && dotnet add package FluentAssertions
  2. Create a test class ending in Tests or Test (xUnit convention) and add using Xunit;
  3. Mark each test method with [Fact] for single-scenario tests or [Theory] with [InlineData()] for parameterized tests
  4. Create Moq mocks with new Mock<IYourInterface>() and set up method returns using .Setup(m => m.Method()).Returns(value)
  5. Instantiate the class under test (SUT), injecting mocked dependencies via constructor
  6. Call the method being tested on the SUT and capture the result
  7. Use FluentAssertions methods like .Should().Be(), .Should().Contain(), .Should().Throw<>() instead of Assert statements
  8. 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

Quick Info

Category.NET / C#
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
dotnetxunittesting

Install command:

curl -o ~/.claude/skills/dotnet-testing.md https://clskills.in/skills/dotnet/dotnet-testing.md

Related .NET / C# Skills

Other Claude Code skills in the same category — free to download.

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.