Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
alvipeo opened this issue Aug 29, 2017 · 3 comments
Closed

Comments

@alvipeo
Copy link

alvipeo commented Aug 29, 2017

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

@SaroTasciyan
Copy link
Contributor

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.

@alvipeo
Copy link
Author

alvipeo commented Aug 30, 2017

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.

@alvipeo alvipeo closed this as completed Aug 30, 2017
@SaroTasciyan
Copy link
Contributor

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".

var mock = new Mock<IFoo>();
mock.Setup(foo => foo.DoSomething("ping")).Returns(true);

Feel free to improve the documents adding new examples ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants