Pregunta

I've just created my first test using AutoFixture. The SUT has the following constructor.

public LoggingService(
    IClientDataProvider clientDataProvider, ... other dependencies...)

The test setup has the following code.

var fixture = new Fixture().Customize(new AutoMoqCustomization());

string ipAddress = "whatever";// fixture.CreateAnonymous<string>();

var clientDataProviderMock = fixture.Freeze<Mock<IClientDataProvider>>();
clientDataProviderMock.Setup(cdp => cdp.IpAddress).Returns(ipAddress);

LoggingService sut = fixture.CreateAnonymous<LoggingService>();

Now, when I examine the contents of sut, I see that the property IpAddress of the injected instance of IClientDataProvider returns null instead of "whatever".

What did I do wrong?


I copied the service and the necessary interfaces to an empty project and then the mocks worked as expected.

Interfaces that are types of constructor arguments of the service in the real project are defined in 3 separate assemblies that have further dependencies. I had several unexpected "Cannot load assembly" errors on the test start because several further assemblies were needed for those directly referenced assemblies. So it seems to be an assembly loading issue.

However, I tried a variation of the test with manual creation of the SUT instance with mock objects created manually using Moq and the test worked as expected

¿Fue útil?

Solución

The solution was pretty surprising. When I was creating my unit test project I added the reference to Moq 4.0 first. AutoFixture was added later and since it seems to require Moq 3.1, I copied that dll directly to bin\Debug. However, the corresponding HintPath element in the project file still pointed to the 4.0 dll. As soon as I changed HintPath to point to the place where Moq 3.1 sits the test started to work properly. So Mark was right with his suggestion but the symptoms were quite different.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top