Stubs and mocks

Stubs are nothing but returned canned responses to calls made during the test; mocks are meant to set expectations:

  • Stubs: In a stubs object, we always get a valid stubbed response. The response doesn’t care what input you provide. In any circumstances, the output will be the same.
  • Mocks: In a mock object, we can test or validate methods that can be called on mocked objects. This is a fake object that validates whether a unit test has failed or passed. In other words, we can say that mock objects are just a replica of our actual object. In the following code, we use the moq framework to implement a mocked object:
        [Fact]        public void Get_Returns_ActionResults()        {            // Arrange            var mockRepo = new Mock<IProductRepository>();

Get Building Microservices with .NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.