I am trying to write some tests, I use xUnit.net, Moq, AutoFixture. I need to inject service to my test method:
[Theory, AutoData]
public void TestSmthCool(IService service)
{
}
The IService
has 3 dependencies which I want to mock. But, If I run the test I get error:
AutoFixture was unable to create an instance from Services.Interfaces.IService because it's an interface.
So, I fixed it in the following way:
[Theory, AutoData]
public void TestSmthCool()
{
var fixture = new Fixture();
fixture.Customize(new AutoMoqCustomization());
fixture.Customizations.Add(
new TypeRelay(
typeof(IService),
typeof(MyService)
)
);
var s= fixture.Create<IService>();
}
But, how to setup TypeRelay
for all tests and inject service via method constructor?