C# 如何添加使用Autofixture创建的模拟的特定实现?
我正在为类编写测试(我们称之为C# 如何添加使用Autofixture创建的模拟的特定实现?,c#,tdd,xunit,autofixture,C#,Tdd,Xunit,Autofixture,我正在为类编写测试(我们称之为Sut),该类通过构造函数注入了一些依赖项。对于这个类,我必须使用参数最多的构造函数,因此我使用了AutoMoqDataAttributeGreedy实现: public class AutoMoqDataAttribute : AutoDataAttribute { public AutoMoqDataAttribute() : base(new Fixture().Customize(new AutoMoqCustomization())) {
Sut
),该类通过构造函数注入了一些依赖项。对于这个类,我必须使用参数最多的构造函数,因此我使用了AutoMoqDataAttributeGreedy
实现:
public class AutoMoqDataAttribute : AutoDataAttribute
{
public AutoMoqDataAttribute() : base(new Fixture().Customize(new AutoMoqCustomization()))
{
}
}
public class AutoMoqDataAttributeGreedy : AutoDataAttribute
{
public AutoMoqDataAttributeGreedy() : base(new Fixture(new GreedyEngineParts()).Customize(new AutoMoqCustomization()))
{
}
}
我的sut的构造函数如下所示:
public class Sut(IInerface1 interface1, IInterface2 interface2, IInterface3 interface3)
{
Interface1 = interface1;
Interface2 = interface2;
Interface3 = interface3;
}
[Theory, AutoMoqDataAttributeGreedy]
public void SomeTest([Frozen]Mock<IInterface1> mock1 ,
Mock<IInterface2> mock2,
Sut sut,
SomOtherdata data)
{
// mock1 and mock2 Setup omitted
// I want to avoid following line
sut.AddSpeficicInterfaceImplementation(new IInterface3TestImplementation());
sut.MethodIWantToTest();
//Assert omitted
}
一个示例测试如下所示:
public class Sut(IInerface1 interface1, IInterface2 interface2, IInterface3 interface3)
{
Interface1 = interface1;
Interface2 = interface2;
Interface3 = interface3;
}
[Theory, AutoMoqDataAttributeGreedy]
public void SomeTest([Frozen]Mock<IInterface1> mock1 ,
Mock<IInterface2> mock2,
Sut sut,
SomOtherdata data)
{
// mock1 and mock2 Setup omitted
// I want to avoid following line
sut.AddSpeficicInterfaceImplementation(new IInterface3TestImplementation());
sut.MethodIWantToTest();
//Assert omitted
}
[理论,自动moqdata属性梯度]
公共测试([冻结]模拟模拟1,
模拟模拟2,
苏苏,
SomOtherdata(数据)
{
//忽略了mock1和mock2设置
//我想避免跟在后面
sut.AddSpeficicInterfaceImplementation(新的IIinterface3TestImplementation());
sut.MethodIWantToTest();
//省略断言
}
问题是我需要一个特定的IInterface3
实现来进行测试,我不想只为我的单元测试向我的SUT(Interface3TestImplementation
)添加一个方法,我也不想重复代码,因为我必须在每个测试中添加这个实例
有没有一种好的、简洁的方法可以将此实现添加到所有使用Autofixture的我的测试/特定测试中?使用您创建的iTexture,您可以调用。针对特定接口进行注册,并在随后使用该接口时提供要使用的对象 e、 g
_fixture=new fixture().自定义(新的AutoMoqCustomization());
_fixture.Register(()=>实现);
您还可以使用mock,这样您就可以在fixture上使用.Freeze,这样您就可以针对接口设置一些预期的调用,而不需要一个完全具体的实例。您可以让AutoFixture为您创建默认实现,并应用您配置的设置
e、 g
var mockedInterface=_fixture.Freeze();
模拟接口
.Setup(x=>x.PropertyOnInterface)
.返回(“某些值”);
您可以让AutoFixture创建一个具体类型的实例,并在每次必须为其任何实现的接口提供值时告诉它使用该实例。下面是一个例子:
[Theory, AutoMoqDataAttributeGreedy]
public void SomeTest(
[Frozen]Mock<IInterface1> mock1,
[Frozen]Mock<IInterface2> mock2,
[Frozen(Matching.ImplementedInterfaces)]IInterface3TestImplementation impl3,
Sut sut)
{
}
[理论,自动moqdata属性梯度]
公开测试(
[冻结]模拟模型1,
[冻结]模拟模型2,
[冻结(匹配.实现接口)]i接口3测试实现3,
Sut Sut)
{
}
在本例中,AutoFixture将创建一个IIinterface3TestImplementation
的实例,并在每次遇到该类型实现的接口时使用它
这意味着,如果Sut
的构造函数具有类型为IInterface3
的参数,AutoFixture将向其传递分配给impl3
参数的同一实例,您可以在测试中使用该实例
另外,除了通过接口之外,还有其他方法将冻结实例与类型和成员进行匹配。如果您想了解更多信息,请查看团队成员。如果您需要将此作为一次性测试,那么Enrico Campidoglio的答案就是最好的选择 如果在所有单元测试中都需要这一点,则可以使用
TypeRelay
自定义Fixture
:
fixture.Customizations.Add(
new TypeRelay(
typeof(IInterface3),
typeof(IInterface3TestImplementation));
这将更改
fixture
,以便无论何时需要IInterface3
,都将创建并使用IInterface3测试实现的实例。不幸的是,我确实需要Interface3
的具体实现。我也不理解你的第一个建议,因为我没有创建任何IFixture。我正在使用AutoFixture通过AutoDataAttribute
自动创建所有模拟和sut
。代码中的这一行将返回一个iTexture,您可以使用它。新夹具().Customize(新的AutoMoqCustomization()哦,好的,我以为你指的是我上面的示例,我没有在代码中创建iTexture
“。问题是我正在通过xUni`理论创建模拟和Sut
,作为实际单元测试中的一个参数。谢谢,这正是我所寻找的。谢谢,我实际上在我的大部分单元测试中都需要它,但我喜欢Enrico的解决方案,它可以减少实际单元测试体中的代码。@zlZimon你可以把上面的内容放进去你在你的AutoMoqDataAttribute
constructor中添加了一个自定义项……哦……不知怎么的,我没有想到。再次感谢。