Challenge: Mocking an External Service

Test yourself by applying a mocking configuration.

In this challenge, we’ll use a stub object to substitute the IUserFacade dependency in the UserDataService class we want to test. We want to expand our test functionality to verify if the methods under test work with different data and that the expected methods on the dependencies are invoked correctly. Therefore, we must apply the following changes:

  • Mock the IUserFacade dependency using the Moq library.

  • Apply the same GetAll() method setup for the GetUserNames_CanGetUserNames() test as currently provided by the stub.

  • For the GetUserNames_CanGetUserEmails(), set up the GetAll() method to return users with different names and return three users instead of two, and to also perform the following:

    • Change the asserts to deal with the updated collection size.

    • Ensure that the presence of each email in the output is verified.

  • In each test, verify that the GetAll() method of this dependency is invoked the correct number of times.

Note: In Moq, we can set up methods even after the Object property is injected into the class under test. As a result, we don’t have to change the initialization of the UserDataService class.

Get hands-on with 1400+ tech skills courses.