Franva
Franva

Reputation: 7077

Moq, the types between Setup() and Returns() doesn't match

I have an interface:

public interface IPeopleService
{
    Task<List<Person>> GetPeople();
}

Here is my code to mock up the return for the request:

List<Person> people = ...;
var mockPeopleService = new Mock<IPeopleService>();
 mockPeopleService.Setup(ps => ps.GetPeople()).Returns<Task<List<Person>>>(Task.FromResult(people));

Error persists no matter what I have done.....

enter image description here

After trying to use ReturnsAsync() it shows me this error:

mockPeopleService.Setup(ps => ps.GetPeople()).ReturnsAsync(people);

enter image description here

Update 2:

enter image description here

Upvotes: 4

Views: 259

Answers (1)

Johnny
Johnny

Reputation: 9519

The problem with your approach is that you are using this method:

IReturnsResult<TMock> Returns<T>(Func<T, TResult> valueFunction);

In your example it doesn't make sense to use that overload. Just drop T. Also might be usefull to use ReturnsAsync.

mockPeopleService.Setup(ps => ps.GetPeople()).ReturnsAsync(people);

Upvotes: 4

Related Questions