I'm trying to write this simple test:
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var postProcessingAction = fixture.Freeze<Mock<IPostProcessingAction>>();
var postProcessor = fixture.Freeze<PostProcessor>();
postProcessor.Process("", "");
postProcessingAction.Verify(action => action.Do());
The Verify
check fails.
The code for postProcessor.Process is
public void Process(string resultFilePath, string jobId)
{
IPostProcessingAction postProcessingAction =
postProcessingActionReader
.CreatePostProcessingActionFromJobResultXml(resultFilePath);
postProcessingAction.Do();
}
postProcessingActionReader
is an interface field initialized through the constructor.
I'm expecting the test to pass but it fails, it turns out the instance of IPostProessingAction
returned from the CreatePostProcessingActionFromJobResultXml
method is not the same instance as returned from fixture.Freeze<>
.
My expectation was that after freezing this Mock object it would inject the underlying mock of the IPostProcessingAction
interface in every place its required as well as make all mock methods returning IPostProcessingAction
return this same object.
Is my expectation about the return value of the mock methods incorrect? Is there a way to change this behavior so that mock methods return the same frozen instance?
As of 3.20.0, you can use
AutoConfiguredMoqCustomization
. This will automatically configure all mocks so that their members' return values are generated by AutoFixture.In other words, it will auto-configure your
postProcessingActionReader
to return the frozenpostProcessingAction
.Just change this:
to this:
You need to
Freeze
theIPostProcessingActionReader
component.The following test will pass:
Assuming that the types are defined as:
In case you use AutoFixture declaratively with the xUnit.net extension the test could be simplified even further:
The
AutoMoqDataAttribute
is defined as: