gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:15:06 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com