ASP.NET Core Web API Interview Question #152

What should be unit tested in a Web API?

Testing, HttpClient, Integration Testing, Background Services & Deployment Senior Advanced

Quick Interview Answer

Unit tests are most valuable around business rules, application services, validation logic, authorization decisions, transformations, and controller behavior that contains meaningful branching.

Detailed Explanation

Unit tests are most valuable around business rules, application services, validation logic, authorization decisions, transformations, and controller behavior that contains meaningful branching.

Framework behavior such as ASP.NET Core routing or model binding generally should not be recreated in unit tests. Integration tests are better suited to verifying how those framework components work together.

Code Example

[Fact]
public async Task Create_RejectsOrder_WhenStockIsInsufficient()
{
    var result = await service.CreateAsync(request, CancellationToken.None);

    Assert.False(result.Succeeded);
    Assert.Equal("Insufficient stock", result.Error);
}