ASP.NET Core Interview Question #206
What should be tested in an ASP.NET Core controller?
Testing, Deployment & Hosting Senior Advanced
Quick Interview Answer
Controller tests should focus on controller responsibilities: interpreting input, invoking application services, and selecting the correct HTTP or MVC result.
Detailed Explanation
Controller tests should focus on controller responsibilities: interpreting input, invoking application services, and selecting the correct HTTP or MVC result.
Complex business rules should normally live in services or domain components and be tested there. A controller unit test can verify outcomes such as ViewResult, RedirectToActionResult, NotFoundResult, or an API status response.
Important routing, filters, model binding, middleware, and serialization behavior is better covered by integration tests because those features are provided by the framework pipeline.
Code Example
[Fact]
public async Task Details_ReturnsNotFound_WhenMissing()
{
var service = new FakeProductService(product: null);
var controller = new ProductsController(service);
var result = await controller.Details(999);
Assert.IsType<NotFoundResult>(result);
}