mardi 29 août 2017

Moq - setup with ReturnsAsync, returning passed in value and get null

ok, I need to mock my IVehicleRecordsRepository to test ASP.NET Core 2.0 MVC Controller. The repo interface:

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

and here's how I try to mock it in the xUnit test:

    [Fact]
        public async Task Added_Post()
        {
            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
       }

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

Aucun commentaire:

Enregistrer un commentaire