You need the Freeze component of IPostProcessingActionReader .
The next test will pass:
[Fact] public void Test() { var fixture = new Fixture() .Customize(new AutoMoqCustomization()); var postProcessingActionMock = new Mock<IPostProcessingAction>(); var postProcessingActionReaderMock = fixture .Freeze<Mock<IPostProcessingActionReader>>(); postProcessingActionReaderMock .Setup(x => x.CreatePostProcessingActionFromJobResultXml( It.IsAny<string>())) .Returns(postProcessingActionMock.Object); var postProcessor = fixture.CreateAnonymous<PostProcessor>(); postProcessor.Process("", ""); postProcessingActionMock.Verify(action => action.Do()); }
Assuming types are defined as:
public interface IPostProcessingAction { void Do(); } public class PostProcessor { private readonly IPostProcessingActionReader actionReader; public PostProcessor(IPostProcessingActionReader actionReader) { if (actionReader == null) throw new ArgumentNullException("actionReader"); this.actionReader = actionReader; } public void Process(string resultFilePath, string jobId) { IPostProcessingAction postProcessingAction = this.actionReader .CreatePostProcessingActionFromJobResultXml(resultFilePath); postProcessingAction.Do(); } } public interface IPostProcessingActionReader { IPostProcessingAction CreatePostProcessingActionFromJobResultXml( string resultFilePath); }
If you use AutoFixture declaratively with the xunit.net extension, the test could be further simplified:
[Theory, AutoMoqData] public void Test( [Frozen]Mock<IPostProcessingActionReader> readerMock, Mock<IPostProcessingAction> postProcessingActionMock, PostProcessor postProcessor) { readerMock .Setup(x => x.CreatePostProcessingActionFromJobResultXml( It.IsAny<string>())) .Returns(postProcessingActionMock.Object); postProcessor.Process("", ""); postProcessingActionMock.Verify(action => action.Do()); }
AutoMoqDataAttribute defined as:
internal class AutoMoqDataAttribute : AutoDataAttribute { internal AutoMoqDataAttribute() : base(new Fixture().Customize(new AutoMoqCustomization())) { } }
source share