Skip to content

Add unit tests for BasketService edge cases and multi-item scenarios#1

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/add-basket-service-unit-tests
Draft

Add unit tests for BasketService edge cases and multi-item scenarios#1
Copilot wants to merge 3 commits into
mainfrom
copilot/add-basket-service-unit-tests

Conversation

Copy link
Copy Markdown

Copilot AI commented Nov 5, 2025

Improves test coverage for BasketService.GetBasket by adding three tests for previously uncovered code paths.

Tests Added

  • GetBasketReturnsEmptyWhenRepositoryReturnsNull - Validates null repository response handling for authenticated users
  • GetBasketHandlesRepositoryExceptionGracefully - Documents that repository exceptions propagate to callers (no try-catch in current implementation)
  • GetBasketReturnsMultipleItemsForValidUserId - Validates correct mapping of multiple basket items including ProductId and Quantity fields

Each test follows existing patterns: Arrange-Act-Assert structure, NSubstitute mocking, and inline comments describing test scenarios.

[TestMethod]
public async Task GetBasketReturnsEmptyWhenRepositoryReturnsNull()
{
    // Arrange: Mock IBasketRepository.GetBasketAsync to return null for a valid user ID
    var mockRepository = Substitute.For<IBasketRepository>();
    mockRepository.GetBasketAsync("1").Returns(Task.FromResult<CustomerBasket>(null));
    // ... setup context with authenticated user
    
    // Act: Call GetBasket with a valid user context
    var response = await service.GetBasket(new GetBasketRequest(), serverCallContext);
    
    // Assert: The response is a CustomerBasketResponse with zero items
    Assert.AreEqual(0, response.Items.Count());
}
Original prompt

Add the following three unit tests to the BasketServiceTests class in tests\Basket.UnitTests\BasketServiceTests.cs to improve test coverage for the BasketService:

  1. GetBasketReturnsEmptyWhenRepositoryReturnsNull

    • Arrange: Mock IBasketRepository.GetBasketAsync to return null for a valid user ID.
    • Act: Call GetBasket with a valid user context.
    • Assert: The response is a CustomerBasketResponse with zero items.
  2. GetBasketHandlesRepositoryExceptionGracefully

    • Arrange: Mock IBasketRepository.GetBasketAsync to throw an exception for a valid user ID.
    • Act: Call GetBasket with a valid user context.
    • Assert: The service either returns an empty basket or handles the exception according to the implementation (e.g., does not throw to the caller).
  3. GetBasketReturnsMultipleItemsForValidUserId

    • Arrange: Mock IBasketRepository.GetBasketAsync to return a CustomerBasket with multiple BasketItem objects for a valid user ID.
    • Act: Call GetBasket with a valid user context.
    • Assert: The response contains the correct number of items and the expected item details.

Each test should follow the Arrange-Act-Assert pattern, use appropriate mocking, and include comments describing the scenario being tested.

Issue created by Visual Studio Copilot


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Nov 5, 2025
Copilot AI and others added 2 commits November 5, 2025 23:25
Co-authored-by: Davsterl <11415265+Davsterl@users.noreply.github.com>
Co-authored-by: Davsterl <11415265+Davsterl@users.noreply.github.com>
Copilot AI changed the title [WIP] Add unit tests for basket service coverage Add unit tests for BasketService edge cases and multi-item scenarios Nov 5, 2025
Copilot AI requested a review from Davsterl November 5, 2025 23:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants