Writing robust unit tests can often feel like a chore, especially when working with complex services like AI layers or cloud databases. But what if your AI-powered coding assistant could do most of the heavy lifting for you? Enter GitHub Copilot. In this blog post, we’ll explore how you can use Copilot to supercharge your .NET unit testing efforts, particularly when working with mocked services like AI layers, Cosmos DB repositories, and API integration testing.
Let’s say you’re testing a service that relies on an AI component (like sentiment analysis). Here’s how you can leverage Copilot to mock it efficiently.
What You Need:
Copilot prompt: Write a unit test that mocks IAIService and tests ReviewService.AnalyzeReviewAsync.
Example:
[Fact]
public async Task AnalyzeReviewAsync_ShouldReturnEnrichedReview()
{
// Arrange
var mockAIService = new Mock<IAIService>();
mockAIService.Setup(x => x.AnalyzeSentimentAsync(It.IsAny<string>()))
.ReturnsAsync(new SentimentResult { Sentiment = "positive" });
var reviewService = new ReviewService(mockAIService.Object, ...);
// Act
var result = await reviewService.AnalyzeReviewAsync("Great product!");
// Assert
Assert.Equal("positive", result.Sentiment);
}
Cosmos DB can be tricky to test, but Copilot helps simplify the setup, whether you’re using the Cosmos DB Emulator or mocking the CosmosClient.
What You Need:
Copilot prompt: Write a unit test for ReviewRepository using a mocked CosmosClient.
Example:
[Fact]
public async Task AddReviewAsync_ShouldStoreReviewInDatabase()
{
// Arrange
var mockContainer = new Mock<Container>();
var mockClient = new Mock<ICosmosDbService>();
mockClient.Setup(x => x.AddReviewAsync(It.IsAny<Review>()))
.ReturnsAsync(new Review { Id = "123", Sentiment = "positive" });
var repo = new ReviewRepository(mockClient.Object);
// Act
var result = await repo.AddReviewAsync(new Review());
// Assert
Assert.Equal("123", result.Id);
}
This helps generate the full test structure, including initialization, mock setup, and expected assertions.
For end-to-end testing, especially with complex dependency chains, Copilot makes integration testing smooth.
What You Need:
Copilot prompt: Write an integration test that posts a review to the API and verifies the response.
[Fact]
public async Task PostReview_ShouldReturnEnrichedReview()
{
var client = _factory.CreateClient();
var content = new StringContent(JsonConvert.SerializeObject(new ReviewDTO
{
Text = "Amazing product!"
}), Encoding.UTF8, "application/json");
var response = await client.PostAsync("/api/review", content);
response.EnsureSuccessStatusCode();
var responseContent = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<Review>(responseContent);
Assert.Equal("positive", result.Sentiment);
}
Copilot will guide you through mocking services, crafting HTTP requests, and asserting responses.
Here are a few pro tips to get the best out of Copilot when writing tests:
Guideline | Description |
Use natural prompts | Be specific: “mock AI response”, “test repo method”, “test POST endpoint” |
Let Copilot fill your test boilerplate | Start with the test method name and let Copilot generate the rest |
Iterate on Copilot’s suggestion | Accept and tweak Copilot’s code, then let it improve upon your changes |
Use [Theory] and inline data | Copilot understands data-driven tests—give it input/output pairs |
Use WebApplicationFactory for integration tests | Helps test actual endpoints with a clean startup configuration |
Add comments to guide Copilot | Inline guidance like // Mock sentiment API improves Copilot’s accuracy |
With Copilot by your side, you no longer need to dread writing tests. It’s like pair programming with an assistant who knows the framework, mocking strategies, and best testing patterns. Whether you’re mocking AI services, emulating Cosmos DB, or writing full-stack integration tests, GitHub Copilot can help turn a time-consuming task into a productive part of development.
Share this:
CloudIQ is a leading Cloud Consulting and Solutions firm that helps businesses solve today’s problems and plan the enterprise of tomorrow by integrating intelligent cloud solutions. We help you leverage the technologies that make your people more productive, your infrastructure more intelligent, and your business more profitable.
LATEST THINKING
INDIA
Chennai One IT SEZ,
Module No:5-C, Phase ll, 2nd Floor, North Block, Pallavaram-Thoraipakkam 200 ft road, Thoraipakkam, Chennai – 600097
© 2025 CloudIQ Technologies. All rights reserved.
Get in touch
Please contact us using the form below
USA
INDIA
Get in touch
Please contact us using the form below
USA
INDIA