Supercharging Unit Testing in .NET with Moq, Bogus, and GitHub Copilot by Yuxin Wang
After attending Microsoft Build 2025, one message stood out: AI is no longer just a coding assistant—it’s becoming an essential part of the entire software development lifecycle, including testing. One area where this is especially impactful is unit testing, a task that’s often tedious, time-consuming, and frequently postponed until later in the development process.
In this blog post, I’ll walk through a practical approach to unit testing in .NET using Moq for mocking, Bogus for generating fake data, and GitHub Copilot to automatically scaffold tests from your code. The key idea is simple but powerful: instead of writing unit tests manually after the backend logic is implemented, we use Copilot and other AI tools to generate tests as soon as API contract is available.
This method helps developers start testing earlier (“shift left”), catch edge cases with the help of AI, and save time on boilerplate code. It aligns perfectly with the direction Microsoft laid out at Build 2025—where they introduced tighter integrations between Copilot, .NET, Visual Studio and Visual Studio Code to supercharge developer productivity and quality.
Whether you’re working on a greenfield project or trying to increase test coverage in an existing codebase, this AI-powered approach can help you build more reliable software—faster and better.
Tools Overview
GitHub Copilot – An AI pair programmer that suggests code and tests based on your current context.
Moq – The most popular mocking framework in .NET.
Bogus – A .NET library for generating fake data for testing.
Shift Left with AI-Assisted Testing
The core idea of this approach is to treat unit test generation as a proactive step, not a reactive one. Instead of waiting until backend logic is fully implemented, developers use AI tools like GitHub Copilot to generate the initial draft of unit tests as soon as the overall coding structure is in place. By feeding interface definitions, DTOs, and service contracts into Copilot, developers can quickly produce basic tests—complete with mocks and fake data using libraries like Moq and Bogus.
Why now?
APIs are increasingly standardized, especially around common CRUD operations, making them easier for AI to understand and generate tests for.
Modern AI tools like Copilot are capable of inferring test logic directly from method signatures, comments, and naming conventions.
Moq and Bogus offer predictable patterns that align well with AI-generated code, reducing the need for manual adjustments.
Example: Generating Unit Tests for a Service
Suppose you have a UserService with a method GetUserByEmail(string email):
public interface IUserRepository
{
User? GetByEmail(string email);
}
public class UserService
{
private readonly IUserRepository _repo;
public UserService(IUserRepository repo)
{
_repo = repo;
}
public User? GetUserByEmail(string email)
{
return _repo.GetByEmail(email);
}
}
Step 1: Let Copilot Generate the Unit Test Skeleton
Start writing your test class, and Copilot will likely autocomplete the rest (or you can start Copilot Chat in VS Code if you use it):
public class UserServiceTests {
private readonly Mock<IUserRepository> _mockRepo;
private readonly UserService _service;
public UserServiceTests() {
_mockRepo = new Mock<IUserRepository>();
_service = new UserService(_mockRepo.Object);
}
[Fact]
public void GetUserByEmail_ReturnsUser_WhenUserExists() {
// Arrange
var fakeUser = new Faker<User>()
.RuleFor(u => u.Id, f => f.Random.Guid())
.RuleFor(u => u.Email, f => f.Internet.Email())
.Generate();
_mockRepo.Setup(r => r.GetByEmail(fakeUser.Email)).Returns(fakeUser);
// Act
var result = _service.GetUserByEmail(fakeUser.Email);
// Assert
result.Should().BeEquivalentTo(fakeUser);
}
}
Step 2: Ask AI for Edge Cases
Once the 'happy path' is created, ask Copilot or ChatGPT for edge cases. Example prompt:
Give me 5 edge cases I should test for GetUserByEmail?
Example AI-generated list:
1. Email is null.
2. Email is an empty string.
3. Email is not found in the repository.
4. Email is in an invalid format.
5. Repository throws an exception.
Step 3: Integrate into Your Dev Workflow
After API contract is finalized, immediately generate test stubs using Copilot.
Share edge case prompts in PR templates or ticket acceptance criteria.
Let Bogus generate fake inputs for almost any class or record.
Evolve tests in sync with backend logic, instead of waiting to write them later.
Benefits
Time saved – Avoid boilerplate writing.
Higher coverage – Don’t miss common edge cases.
Faster onboarding – Junior devs can follow a generated pattern.
Better quality – Write tests early, reduce bugs later.
Final Thoughts
AI tools like GitHub Copilot are already reshaping how we write software. When paired with powerful libraries like Moq and Bogus, they enable us to shift testing left—starting earlier, accelerating development, and minimizing repetitive work. By adopting this pattern, teams can quickly bootstrap test coverage before backend logic is fully implemented, allowing developers to focus their time where it matters most: handling edge cases, testing integrations, and validating complex business logic.
Happy testing!
Bonus take away: Want to go further? Combine this workflow with AutoFixture, FluentAssertions, or snapshot testing tools like Verify for a full-fledged AI-powered test strategy.