简体   繁体   中英

Unit Testing a Service with Moq & xUnit

Sorry, this is likely a very amateur question, but I am struggling to understand how to use Moq properly. I am quite new to unit testing as a whole, but I think I'm starting to get the hang of it.

So here's my question... I have this snippet of code below which is using a TestServer in Visual Studio that I using for am Unit Testing... I'm trying to mock IGamesByPublisher so that my test is not reliant on data in the repository (or would it be better to mock GamesByPublisher ?... Or do I need to do both?)

public static TestServerWithRepositoryService => new TestServer(services =>
{
    services.AddScoped<IGamesByPublisher, GamesByPublisher(); 
}).AddAuthorization("fake.account", null);


[Fact] // 200 - Response, Happy Path
public async Task GamesByPublisher_GamesByPublisherLookup_ValidRequestData_Produces200()
{

    // Arrange
    var server = ServerWithRepositoryService;

    // Act
    var response = await server.GetAsync(Uri);

    // Assert
    Assert.NotNull(response);
    Assert.Equal(HttpStatusCode.OK, response.StatusCode);

}

Here is the IGamesByPublisher

public interface IGamesByPublisher interface.
{
    Task<IEnumerable<Publisher>> Execute(GamesByPublisherQueryOptions options);
    }
}

I tried

public static TestServerWithRepositoryService => new TestServer(services =>
{
    services.AddScoped<Mock<IGamesByPublisher>, Mock<GamesByPublisher>>(); 
}).AddAuthorization("fake.account", null);

And then I tried

// Not exactly what I attempted, but that code is long gone... 
var mock = new Mock<IGamesByPublisher >();
var foo = new GamesByPublisherQueryOptions();
mock.Setup(x => x.Execute(foo)).Returns(true);

I didn't really find great documentation on using Moq, just the quick start guide on GitHub, which I wasn't sure how to apply (probably my own level of experience at fault there...).

I am obviously missing some fundamentals on using Moq...

You were close.

public static TestServerWithRepositoryService => new TestServer(services => {
    var mock = new Mock<IGamesByPublisher>();

    var publishers = new List<Publisher>() {
        //...populate as needed
    };

    mock
        .Setup(_ => _.Execute(It.IsAny<GamesByPublisherQueryOptions>()))
        .ReturnsAsync(() => publishers);
    services.RemoveAll<IGamesByPublisher>();
    services.AddScoped<IGamesByPublisher>(sp => mock.Object); 
}).AddAuthorization("fake.account", null);

The above creates the mock, sets up its expected behavior to return a list of publishers any time Execute is invoked with a GamesByPublisherQueryOptions .

It then removes any registrations of the desired interface to avoid conflicts and then registers the service to return the mock any time the interface is requested to be resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM