The Intent.AI.UnitTests module uses AI to automatically implement unit tests for the Handler method of a Command or Query, based on full context from generated code and model metadata provided by Intent Architect.Unlike manual test writing, this module analyzes your handler implementation, dependencies, and domain logic to generate comprehensive test coverage including happy paths, edge cases, and error scenarios.
To reliably generate unit tests, this module should be used in conjunction with the Intent.UnitTesting module. The Intent.UnitTesting module generates the test infrastructure (e.g., test project and dependencies), while Intent.AI.UnitTests handles the test implementation.
To use this feature, ensure that the required User Settings have been completed — including a valid API key for your selected AI provider.
Before using Generate Unit Tests with AI, make sure:
Generated Code is up-to-date: Run the Software Factory to apply all outstanding code changes
Command/Query is mapped: Ensure the Command or Query is associated with the appropriate Entity using a Create Entity, Update Entity, or Query Entity action
Unit Test infrastructure is Set Up: Ensure the Intent.UnitTesting module has been installed, and that the Command or Query has the Unit Test stereotype applied
Handler is implemented: The handler should have actual logic to test (not just an empty method)
While Intent Architect supplies a default prompt and relevant file context to the AI provider, you can optionally provide additional context to refine the unit tests generated.Add custom instructions:Example prompts:
Include tests for concurrent access scenarios with the same customer ID.
Add performance tests to ensure the query completes within 100ms.
Test all possible order status transitions.
It’s recommended to try the default implementation first. If needed, rerun with added context to improve results.
AI responses are not deterministic — each execution may produce different results. Use the additional context prompt to guide the AI toward your desired test coverage.
public async Task<Guid> Handle(CreateOrderCommand request, CancellationToken cancellationToken){ var customer = await _customerRepository.FindByIdAsync(request.CustomerId, cancellationToken); if (customer == null) throw new NotFoundException($"Customer {request.CustomerId} not found"); var order = new Order { CustomerId = request.CustomerId, OrderDate = DateTime.UtcNow, Status = OrderStatus.Pending }; _orderRepository.Add(order); await _orderRepository.UnitOfWork.SaveChangesAsync(cancellationToken); return order.Id;}
AI-Generated Tests:
CreateOrderCommandHandlerTests.cs
public class CreateOrderCommandHandlerTests{ private readonly Mock<IOrderRepository> _orderRepositoryMock; private readonly Mock<ICustomerRepository> _customerRepositoryMock; private readonly CreateOrderCommandHandler _handler; public CreateOrderCommandHandlerTests() { _orderRepositoryMock = new Mock<IOrderRepository>(); _customerRepositoryMock = new Mock<ICustomerRepository>(); _handler = new CreateOrderCommandHandler( _orderRepositoryMock.Object, _customerRepositoryMock.Object ); } [Fact] public async Task Handle_ValidRequest_CreatesOrder() { // Arrange var customerId = Guid.NewGuid(); var customer = new Customer { Id = customerId, Name = "Test Customer" }; _customerRepositoryMock .Setup(x => x.FindByIdAsync(customerId, default)) .ReturnsAsync(customer); var command = new CreateOrderCommand { CustomerId = customerId }; // Act var result = await _handler.Handle(command, CancellationToken.None); // Assert Assert.NotEqual(Guid.Empty, result); _orderRepositoryMock.Verify( x => x.Add(It.Is<Order>(o => o.CustomerId == customerId && o.Status == OrderStatus.Pending )), Times.Once ); } [Fact] public async Task Handle_NonExistentCustomer_ThrowsNotFoundException() { // Arrange var customerId = Guid.NewGuid(); _customerRepositoryMock .Setup(x => x.FindByIdAsync(customerId, default)) .ReturnsAsync((Customer?)null); var command = new CreateOrderCommand { CustomerId = customerId }; // Act & Assert var exception = await Assert.ThrowsAsync<NotFoundException>( () => _handler.Handle(command, CancellationToken.None) ); Assert.Contains(customerId.ToString(), exception.Message); } [Fact] public async Task Handle_ValidRequest_SavesChanges() { // Arrange var customerId = Guid.NewGuid(); _customerRepositoryMock .Setup(x => x.FindByIdAsync(customerId, default)) .ReturnsAsync(new Customer { Id = customerId }); var command = new CreateOrderCommand { CustomerId = customerId }; // Act await _handler.Handle(command, CancellationToken.None); // Assert _orderRepositoryMock.Verify( x => x.UnitOfWork.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once ); }}
public async Task<CustomerDto> Handle(GetCustomerByIdQuery request, CancellationToken cancellationToken){ var customer = await _customerRepository.FindByIdAsync(request.Id, cancellationToken); if (customer == null) throw new NotFoundException($"Customer {request.Id} not found"); return new CustomerDto { Id = customer.Id, Name = customer.Name, Email = customer.Email, TotalOrders = customer.Orders.Count };}
AI-Generated Tests:
GetCustomerByIdQueryHandlerTests.cs
public class GetCustomerByIdQueryHandlerTests{ private readonly Mock<ICustomerRepository> _customerRepositoryMock; private readonly GetCustomerByIdQueryHandler _handler; public GetCustomerByIdQueryHandlerTests() { _customerRepositoryMock = new Mock<ICustomerRepository>(); _handler = new GetCustomerByIdQueryHandler(_customerRepositoryMock.Object); } [Fact] public async Task Handle_ExistingCustomer_ReturnsCustomerDto() { // Arrange var customerId = Guid.NewGuid(); var customer = new Customer { Id = customerId, Name = "John Doe", Email = "[email protected]", Orders = new List<Order> { new Order(), new Order() } }; _customerRepositoryMock .Setup(x => x.FindByIdAsync(customerId, default)) .ReturnsAsync(customer); var query = new GetCustomerByIdQuery { Id = customerId }; // Act var result = await _handler.Handle(query, CancellationToken.None); // Assert Assert.NotNull(result); Assert.Equal(customerId, result.Id); Assert.Equal("John Doe", result.Name); Assert.Equal("[email protected]", result.Email); Assert.Equal(2, result.TotalOrders); } [Fact] public async Task Handle_NonExistentCustomer_ThrowsNotFoundException() { // Arrange var customerId = Guid.NewGuid(); _customerRepositoryMock .Setup(x => x.FindByIdAsync(customerId, default)) .ReturnsAsync((Customer?)null); var query = new GetCustomerByIdQuery { Id = customerId }; // Act & Assert var exception = await Assert.ThrowsAsync<NotFoundException>( () => _handler.Handle(query, CancellationToken.None) ); Assert.Contains(customerId.ToString(), exception.Message); } [Fact] public async Task Handle_CustomerWithNoOrders_ReturnsZeroOrders() { // Arrange var customerId = Guid.NewGuid(); var customer = new Customer { Id = customerId, Name = "Jane Doe", Email = "[email protected]", Orders = new List<Order>() }; _customerRepositoryMock .Setup(x => x.FindByIdAsync(customerId, default)) .ReturnsAsync(customer); var query = new GetCustomerByIdQuery { Id = customerId }; // Act var result = await _handler.Handle(query, CancellationToken.None); // Assert Assert.Equal(0, result.TotalOrders); }}