lundi 26 octobre 2015

When unit testing, how do I mock a return null from async method?

Normally, I mock my repo like so:

var repository = new Mock<ISRepository>();
repository.Setup(r => r.GetMemberAsync(email))
    .Returns(Task.FromResult(new Member
    {
        FirstName = firstName,
        LastName = lastName
    }));

But, in my code, I check to see if the member is not found, i.e. GetMemberAsync returns null. How do I mock this?

I tried:

var repository = new Mock<ISRepository>();
repository.Setup(r => r.GetMemberAsync(email))
    .Returns(Task.FromResult<object>(null));

but I get a compile error.

Aucun commentaire:

Enregistrer un commentaire