C# 使用NSubstitute、AutoFixture和AutoFixture.AutoNSubstitute替换方法返回
我试图使用NSubstitute(1.8.2)、AutoFixture(3.33)和AutoFixture.AutoNSubstitute(3.33),如下所示: 我有一个poco,它有一个方法,并在我的应用程序中使用。为了测试我的应用程序,我需要模拟poco及其从方法返回的内容 这是poco(根据我们的实际模型简化) 这个测试呢C# 使用NSubstitute、AutoFixture和AutoFixture.AutoNSubstitute替换方法返回,c#,autofixture,nsubstitute,C#,Autofixture,Nsubstitute,我试图使用NSubstitute(1.8.2)、AutoFixture(3.33)和AutoFixture.AutoNSubstitute(3.33),如下所示: 我有一个poco,它有一个方法,并在我的应用程序中使用。为了测试我的应用程序,我需要模拟poco及其从方法返回的内容 这是poco(根据我们的实际模型简化) 这个测试呢 [TestMethod] public void SampleTest() { var fixture = new Fixture().Customize(n
[TestMethod]
public void SampleTest()
{
var fixture = new Fixture().Customize(new AutoConfiguredNSubstituteCustomization());
var substitute = fixture.Create<SamplePoco>();
var formattedString = fixture.Create<string>();
substitute.GetFormattedString().Returns(formattedString);
// ... test goes here
}
[TestMethod]
公共无效样本测试()
{
var fixture=new fixture().Customize(新的自动配置nsubstitutecustomization());
var substitute=fixture.Create();
var formattedString=fixture.Create();
substitute.GetFormattedString().Returns(formattedString);
//…测试在这里进行
}
我已经使用为AutoFixture.AutoMoq找到的代码示例确定了这一点,因为我无法找到AutoFixture.AutoNSubstitute的任何特定示例
但是,此操作失败,并出现以下错误:
NSSubstitute.Exceptions.CouldNotSetReturnDueToNoLastCallException:
找不到要从中返回的呼叫
我做错了什么
按预期使用NSubstitute工程:
[TestMethod]
public void SampleTest()
{
var fixture = new Fixture().Customize(new AutoConfiguredNSubstituteCustomization());
var substitute = Substitute.For<SamplePoco>();
var formattedString = fixture.Create<string>();
substitute.GetFormattedString().Returns(formattedString);
// ... test goes here
}
[TestMethod]
公共无效样本测试()
{
var fixture=new fixture().Customize(新的自动配置nsubstitutecustomization());
var substitute=substitute.For();
var formattedString=fixture.Create();
substitute.GetFormattedString().Returns(formattedString);
//…测试在这里进行
}
但是,我想使用AutoFixture,因为实际的poco有更多的属性和方法,并且这些方法比这个简单的示例更复杂。正如您所写的,
SamplePoco
类是一个poco
,所以当您在第一次测试中调用fixture.Create()
,您将获得由AutoFixture
使用反射并调用默认构造函数创建的SamplePoco
类的新实例
我认为您的第一个测试很好,AutoFixture
还创建了一个SampleString
因此,您不需要存根GetFormattedString
方法
从测试的角度来看,您的GetFormattedString
方法包含行为,因此您应该以这种方式测试它:
我想你有点误解了。我不想测试
SamplePoco
。我正在测试一个不同的类,它使用SamplePoco
并且不希望调用SamplePoco.GetFormattedString
,因此我希望SamplePoco.GetFormattedString
在测试另一个类时返回模拟值。但是为什么不希望调用SamplePoco.GetFormattedString
?存根SamplePoco.GetFormattedString
的原因是什么?这只是一个简单的例子。实际的GetFormattedString
做的不止这些,还包括在测试外部类时我不希望发生的进一步调用。因此,使用AutoDataAttribute
和SubstituteAttribute
的解决方案似乎对您不合适?。另外,请看关于替换类的警告注释。我将查看属性链接并报告,我只是快速回答了您的评论。我知道类的问题:)这就是我知道如何使方法虚拟的原因
[TestMethod]
public void SampleTest()
{
var fixture = new Fixture().Customize(new AutoConfiguredNSubstituteCustomization());
var substitute = Substitute.For<SamplePoco>();
var formattedString = fixture.Create<string>();
substitute.GetFormattedString().Returns(formattedString);
// ... test goes here
}
public void SampleTest()
{
var fixture = new Fixture().Customize(new AutoConfiguredNSubstituteCustomization());
var poco = fixture.Create<SamplePoco>();
Assert.AreEquals(string.Format("{0} {1}", poco.SampleString, "formatted"), poco.GetFormattedString());
}
public class SamplePocoSubstituteCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Register(() => Substitute.For<SamplePoco>());
}
}
[TestMethod]
public void SampleTest()
{
var fixture = new Fixture().Customize(new AutoConfiguredNSubstituteCustomization())
.Customize(new SamplePocoSubstituteCustomization());
var substitute = fixture.Create<SamplePoco>();
var formattedString = fixture.Create<string>();
substitute.GetFormattedString().Returns(formattedString);
// ... test goes here
}