mardi 29 août 2017

Setup with ReturnsAsync, need to return passed in value but getting null

I need to mock my IVehicleRecordsRepository for some tests. So here's the repo interface:

public interface IVehicleRecordsRepository
{
    Task<VehicleRecord> StoreAsync(VehicleRecord veh, Guid? userId = null);
    //...
}

and now I try to mock it in the xUnit test so StoreMethod() should return the same value that was passed in as a parameter. Here's the test that tests this scenario:

[Fact]
public async Task ShouldGetValueFromMockedMethod()
{
    var mockRepo = new Mock<IVehicleRecordsRepository>();
    mockRepo
        .Setup(repo => repo.StoreAsync(It.IsAny<VehicleRecord>(), Guid.NewGuid()))
        .ReturnsAsync((VehicleRecord veh, Guid userId) => veh)
        // tried this too -> .Returns((VehicleRecord veh, Guid userId) => Task.FromResult(veh))
        ;

    VehicleRecord vr = new VehicleRecord(newVehicle, Guid.NewGuid());
    var testVeh = await mockRepo.Object.StoreAsync(vr);
    Assert.NotNull(testVeh); // ====> FAILS HERE
    Assert.AreEqual(vr, testVeh);
}

So, how can I get the same value I passed into StoreAsync() in return ?

Moq version: 4.7.99.0

Aucun commentaire:

Enregistrer un commentaire