moq - AutoFixture:模拟方法不返回卡住的实例

标签 moq autofixture

我正在尝试编写这个简单的测试:

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());
Verify检查失败。
postProcessor.Process 的代码是

public void Process(string resultFilePath, string jobId)
{
    IPostProcessingAction postProcessingAction =
        postProcessingActionReader
            .CreatePostProcessingActionFromJobResultXml(resultFilePath);

    postProcessingAction.Do();
}
postProcessingActionReader是通过构造函数初始化的接口(interface)字段。

我期待测试通过,但它失败了,结果是 IPostProessingAction 的实例从 CreatePostProcessingActionFromJobResultXml 返回方法与 fixture.Freeze<> 返回的实例不同.

我的期望是,在卡住这个 Mock 对象后,它会注入(inject) IPostProcessingAction 的底层模拟。在每个地方都需要接口(interface),并使所有模拟方法返回 IPostProcessingAction返回相同的对象。

我对模拟方法的返回值的期望不正确吗?
有没有办法改变这种行为,以便模拟方法返回相同的卡住实例?

最佳答案

您需要 Freeze IPostProcessingActionReader零件。

以下测试将通过:

[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());
}

假设类型定义为:

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);
}

如果您使用 AutoFixture declaratively与 xUnit.net extension测试可以进一步简化:

[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定义为:

internal class AutoMoqDataAttribute : AutoDataAttribute
{
    internal AutoMoqDataAttribute()
        : base(new Fixture().Customize(new AutoMoqCustomization()))
    {
    }
}

关于moq - AutoFixture:模拟方法不返回卡住的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14709025/

相关文章:

c# - 如何强制 AutoFixture 创建 ImmutableList

c# - 使用例如创建模拟对象和匿名对象的混合体最小起订量和 AutoFixture?

c# - 如何让 AutoFixture AutoMoq 从实例化对象中的注入(inject)服务返回结果?

c# - 模拟单元测试在 AsNoTracking() 上返回 ArgumentNullException

c# - 使用 List<string>、Autofixture 创建测试数据

c# - 无法掌握 Freeze/Inject/Register 之间的区别

c# - 如何使用 Moq 构建此表达式而不会出现方法组错误?

Silverlight:单元测试,SL4 的推荐框架?

c# - 单元测试依赖于 UserManager<TUser> 的 Controller 的最佳实践?

c# - 基于 Moq 的单元测试中的 TargetParameterCountException