ASP.NET Core MVC Interview Question #156

What is the difference between a mock, stub, and fake in MVC tests?

Testing, AJAX/JavaScript Integration, Localization & Advanced MVC Senior Advanced

Quick Interview Answer

These are categories of test doubles. A stub supplies predetermined responses, a mock is commonly used to verify interactions or expectations, and a fake provides a lightweight working implementation such as an in-memory repository designed for tests.

Detailed Explanation

These are categories of test doubles. A stub supplies predetermined responses, a mock is commonly used to verify interactions or expectations, and a fake provides a lightweight working implementation such as an in-memory repository designed for tests.

Terminology varies between teams and libraries, so the important point is why the test double exists. Avoid excessive mocking of implementation details because it can make tests brittle and tightly coupled to internal structure.

Code Example

var service = new Mock<IProductService>();

service.Setup(x => x.ExistsAsync(10, token))
       .ReturnsAsync(true);

service.Verify(
    x => x.ExistsAsync(10, token),
    Times.Once);