Customizing AutoMock with AutoFixture Data Systems

Well, as was said in my other question , it AutoMoqdoes not use AutoFixture by default. This is great and easy to solve by setting up and installing ReturnsUsingFixture.

But can this be adjusted using Auto Fixture Data Theory?

So, we have a custom attribute AutoDataAttribute, which I will name [MyAutoData]. And there we call and configure a set of settings, such as AutoConfiguredMoqCustomization, configure it to create webapi controllers and register many user-generated generators. Thus, we were able to pull the FULL configuration template into some basic configuration files. We even set up an attribute MyAutoDatafor system tests, so if you ask, say, Id<Account>it will go and create a new account using the actual webapi calls and return a valid account identifier.

But how can you handle the fact that it AutoMoqreturns to set up a method ? Here is an example:

    [Theory, MyAutoData]
    public async Task Test(Mock<ICqrsService> mockService, TheRequest request) 
    {
        mockService.Setup(service => service.CreateAsync<TheRequest>(It.IsAny<TheRequest>(), It.IsAny<CancellationToken>()))
        .ReturnsAsync(result); // or similar car with ReturnUsingFixture
        /* now we can test */
    }

MyAutoData ( , ). AutoMoq , . .

, AutoFixture , ? AutoMoq, .ReturnsUsingFixture(fixture)? , ?

+4
2

IPostprocessComposer<T>.Do(Action<T>) .

, Test Double , AutoFixture:

public class FakeServiceCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Customize<Mock<IService>>(composer =>
            composer.Do(fake =>
                fake.Setup(service => service.Create<Something>())
                    .ReturnsUsingFixture(fixture);
    }
}
+3

FWIW, , AutoConfiguredMoqCustomization . AutoFixture, , , .

, - , AutoConfiguredMoqCustomization ; , , . , , AutoFixture , .


, , GOOS, , . , , SUT . SUT, .

SUT, AutoFixture?

+3

Source: https://habr.com/ru/post/1612627/


All Articles