You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
IVehicleRecordsRepository mock is set up with a specific (new) guid, which I suppose is not stored and reused in anywhere. When the mocked method is called without the optional parameter, null is passed for the nullable guid value instead of the one that was set up.
If the guid passed to mocked method is not important for you, you could just use It.IsAny<Guid?>, otherwise instead of inlining the guid value, use a local variable for it and pass that variable as second argument during invocation of the mocked method.
Oh, thank you very much! It's not really easy to understand from the docs and examples. Probably, it's better to have such an example somewhere in the docs.
Actually it's explained in detail within the Matching Arguments section of the Quickstart. However, it'd be nice to have it explained right in the beginning that the mocked method will return true only when it's called with "ping".
I need to mock my
IVehicleRecordsRepository
for some tests. So here's the repo interface: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:So, how can I get the same value I passed into
StoreAsync()
in return ?Moq version: 4.7.99.0
The text was updated successfully, but these errors were encountered: