ASP.NET Core Interview Question #201

What is unit testing in ASP.NET Core applications?

Testing, Deployment & Hosting Senior Advanced

Quick Interview Answer

Unit testing verifies a small unit of application behavior in isolation, typically a method or class. Dependencies such as repositories, external APIs, clocks, or message senders are replaced with fakes, stubs, or mocks when appropriate.

Detailed Explanation

Unit testing verifies a small unit of application behavior in isolation, typically a method or class. Dependencies such as repositories, external APIs, clocks, or message senders are replaced with fakes, stubs, or mocks when appropriate.

Good unit tests are fast, deterministic, and focused on observable behavior. Business and application services are usually better unit-test targets than framework wiring.

Dependency injection improves testability because a class can receive test implementations of its dependencies without starting the full ASP.NET Core host.

Code Example

[Fact]
public async Task GetTotalAsync_ReturnsRepositoryTotal()
{
    var repository = new FakeOrderRepository(total: 125m);
    var service = new OrderService(repository);

    var result = await service.GetTotalAsync();

    Assert.Equal(125m, result);
}