Unit Testing with GitHub Copilot: Mocking AI Services, Cosmos DB, and APIs

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. 

1. Mocking the AI Service Layer for Unit Tests 

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: 

  • An IAIService interface (e.g., Sentiment Analysis) 
  • A unit testing framework like xUnit, NUnit, or MSTest 
  • A mocking library such as Moq 

 
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); 

} 

2. Testing Cosmos DB Repositories with In-Memory Emulators 

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: 

  • Cosmos DB Emulator or a mocked CosmosClient 
  • A repository pattern to abstract the data layer 
  • Test setup/teardown logic for clean test states 

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. 

3. API Testing with Mock Cosmos Responses + Sample AI Outputs 

For end-to-end testing, especially with complex dependency chains, Copilot makes integration testing smooth. 

What You Need: 

  • ASP.NET Core integration tests using WebApplicationFactory<> 
  • Mocked Cosmos and AI services injected via TestStartup.cs 
  • Dependency injection is managed using mock services. 

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. 

Best Practices: Using GitHub Copilot for Unit Testing 

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 

Conclusion 

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:

Want help modernizing

your applications?

Let’s Talk

    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. 

    US

    626 120th Ave NE, Suite B102, Bellevue,

    WA, 98005.

    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

      626 120th Ave NE, Suite B102, Bellevue, WA, 98005.

      +1 (206) 203-4151

      INDIA

      Chennai One IT SEZ,

      Module No:5-C, Phase ll, 2nd Floor, North Block, Pallavaram-Thoraipakkam 200 ft road, Thoraipakkam, Chennai – 600097

      +91-044-43548317

      Get in touch

      Please contact us using the form below

        USA

        626 120th Ave NE, Suite B102, Bellevue, WA, 98005.

        +1 (206) 203-4151

        INDIA

        Chennai One IT SEZ,

        Module No:5-C, Phase ll, 2nd Floor, North Block, Pallavaram-Thoraipakkam 200 ft road, Thoraipakkam, Chennai – 600097

        +91-044-43548317