Search code examples
c#.netunit-testingmoq

Moq does not contain a definition for ReturnAsync?


I am trying to mock some API calls to a third-party service for unit testing purposes. I really just want this mocked function to return the same RestEase.Response<...> each time.

// Setup
var VeracrossMock = new Mock<IVeracrossAPI>(MockBehavior.Strict);
Func<List<VeracrossStudent>> func = () => new List<VeracrossStudent>() { new VeracrossStudent() { First_name = "Bob", Last_name = "Lob" } };
RestEase.Response<List<VeracrossStudent>> resp = new RestEase.Response<List<VeracrossStudent>>("", new HttpResponseMessage(HttpStatusCode.OK), func);

// Problem is on the line below
VeracrossMock.Setup(api => api.GetStudentsAsync(1, null, CancellationToken.None)).ReturnsAsync<RestEase.Response<List<VeracrossStudent>>>(resp);

It gives me a red underline and then claims the ReturnsAsync doesn't exist, or at least not with the arguments that I've given it.

Error CS1929 'ISetup<IVeracrossAPI, Task<Response<List<VeracrossStudent>>>>' does not contain a definition for 'ReturnsAsync' and the best extension method overload 'SequenceExtensions.ReturnsAsync<Response<List<VeracrossStudent>>>(ISetupSequentialResult<Task<Response<List<VeracrossStudent>>>>, Response<List<VeracrossStudent>>)' requires a receiver of type 'ISetupSequentialResult<Task<Response<List<VeracrossStudent>>>>'

How am I supposed to be using ReturnsAsync? Clueless as to how to mock this.


Solution

  • The generic argument being used does not match the arguments of the member being mocked.

    Remove the generic argument

    VeracrossMock
        .Setup(_ => _.GetStudentsAsync(1, null, CancellationToken.None))
        .ReturnsAsync(resp);
    

    and the method will infer the desired generic arguments based on the member being mocked.