- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我以前在单元测试中使用 Moq 和 AutoMoqer,但我的团队已决定改为使用 NSubstitute。我们大量使用 DI,所以我希望能够请求一个目标进行测试,并让该目标自动将所有模拟对象提供给其构造函数,或者换句话说,一个传递模拟的 DI 容器。我还想根据需要修改那些模拟对象。
使用 Moq/AutoMoq/MSTest 的示例
[TestMethod]
public void ReturnSomeMethod_WithDependenciesInjectedAndD1Configured_ReturnsConfiguredValue()
{
const int expected = 3;
var diContainer = new AutoMoq.AutoMoqer();
var mockedObj = diContainer.GetMock<IDependency1>();
mockedObj
.Setup(mock => mock.SomeMethod())
.Returns(expected);
var target = diContainer.Resolve<MyClass>();
int actual = target.ReturnSomeMethod();
Assert.AreEqual(actual, expected);
}
public interface IDependency1
{
int SomeMethod();
}
public interface IDependency2
{
int NotUsedInOurExample();
}
public class MyClass
{
private readonly IDependency1 _d1;
private readonly IDependency2 _d2;
//please imagine this has a bunch of dependencies, not just two
public MyClass(IDependency1 d1, IDependency2 d2)
{
_d1 = d1;
_d2 = d2;
}
public int ReturnSomeMethod()
{
return _d1.SomeMethod();
}
}
由于我的问题措辞不当,而且我进行了更多研究,因此我找到了一种使用 NSubstitute/AutofacContrib.NSubstitute/XUnit 使其正常工作的方法:
[Fact]
public void ReturnSomeMethod_WithDependenciesInjectedAndD1Configured_ReturnsConfiguredValue()
{
const int expected = 3;
var autoSubstitute = new AutoSubstitute();
autoSubstitute.Resolve<IDependency1>().SomeMethod().Returns(expected);
var target = autoSubstitute.Resolve<MyClass>();
int actual = target.ReturnSomeMethod();
Assert.Equal(actual, expected);
}
public interface IDependency1
{
int SomeMethod();
}
public interface IDependency2
{
int NotUsedInOurExample();
}
public class MyClass
{
private readonly IDependency1 _d1;
private readonly IDependency2 _d2;
//please imagine this has a bunch of dependencies, not just two
public MyClass(IDependency1 d1, IDependency2 d2)
{
_d1 = d1;
_d2 = d2;
}
public int ReturnSomeMethod()
{
return _d1.SomeMethod();
}
}
我仍然有我原来的问题。我如何使用 AutoFixture.AutoNSubstitute 作为 DI 容器来做到这一点?
最佳答案
您可以将 AutoFixture 变成 Auto-Mocking Container具有各种动态模拟库,包括 NSubstitute。
重写 OP 测试就这么简单:
[Fact]
public void ReturnSomeMethod_UsingAutoFixtureAutoNSubstitute()
{
const int expected = 3;
var fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
fixture.Freeze<IDependency1>().SomeMethod().Returns(expected);
var target = fixture.Create<MyClass>();
var actual = target.ReturnSomeMethod();
Assert.Equal(actual, expected);
}
在这里,我使用了 xUnit.net而不是 MSTest,但翻译它应该是微不足道的。
这里的关键是 Freeze
方法,它实质上将类型参数的生命周期从 transient 更改为 singleton。这意味着在调用 Freeze
之后,所有其他时间特定的 Fixture
实例必须创建一个 IDependency
对象,它将重用该对象它卡住了。
Freeze
还返回它刚刚卡住的对象,这使您可以轻松地在其中“点”入 - 在本例中使用 NSubstitute 的 API。
关于c# - 在使用 nSubstitute 和 Autofixture 作为 DI 容器的单元测试中,如何获取模拟对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46596161/
我有一个用“NSubstitute”模拟的接口(interface),它包含返回 concreate 类的属性,即返回值不是接口(interface)。例如 public interface ISom
我有一个用“NSubstitute”模拟的接口(interface),其中包含返回 concreate 类的属性,即返回值不是接口(interface)。例如 public interface ISo
我有一个用“NSubstitute”模拟的接口(interface),其中包含返回 concreate 类的属性,即返回值不是接口(interface)。例如 public interface ISo
我正在使用 NSubstitute 通过 PartsOf() 来模拟一个类方法(我需要一些方法来工作)。它看起来像这样: var mock = Substitute.ForPartsOf(); moc
使用 NSubstitute。对于某些测试,我想断言替代者没有收到任何调用。我可以对界面中的每个方法使用 DidNotReceiveWithAnyArgs(),但这很乏味而且不够健壮(如果将新方法添加
使用 NSubstitute。对于某些测试,我想断言替代者没有收到任何调用。我可以对界面中的每个方法使用 DidNotReceiveWithAnyArgs(),但这很乏味而且不够健壮(如果将新方法添加
我有一个用 NSubstitute 伪造的对象,它有一个被调用两次的方法。我想验证该方法实际上已被调用两次(且仅调用两次)。我浏览了文档和谷歌,但没有运气。任何帮助,将不胜感激。谢谢。 最佳答案 NS
void ABC() { var foo = Substitute.For(); foo.When(x => x.Bar()).Do(x => counter++); ....
我有一个看起来像这样的类(class): public class MyClass { public virtual bool A() { return 5 ();
我在使用 NSubstitute 模拟带有输出参数的方法时遇到过这种情况。我不确定如何最好地用文本解释它,所以我将使用一些人为的示例和测试用例...... 在这个人为的示例中,我将使用 IDictio
void ABC() { var foo = Substitute.For(); foo.When(x => x.Bar()).Do(x => counter++); ....
我有一个看起来像这样的类(class): public class MyClass { public virtual bool A() { return 5 ();
对于下面的代码,我得到了这个断言失败,不知道为什么: Assert.AreEqual failed. Expected:. Actual:. public interface IA { voi
我对 NSubstitute、模拟和单元测试总体来说是新手。 我正在尝试使用 NSubstitute 删除测试类中的一些依赖项,但模拟对象中的方法的行为并不符合我的预期(根据我的配置方式)。以下是我在
假设我有一个类: public abstract class Test { internal abstract int Prop { get; } } 现在,我
我在单元测试中创建了 Person 和 AddressBook 类的替代品。AddressBook 类包含 Person 类型和名称的属性:SamplePerson。 public interface
我有一个测试,其中 NSubstitute 检查假类中的错误调用。当我像下面的代码一样进行测试时,Received(...) 方法会检查值 factory.featureClassName 是否返回一
我有一个带有以下声明的接口(interface): void MapServiceMessages(IEnumerable serviceMessages, List responseMessages
大家好,我是 NSubstitute 框架的新手。我正在尝试测试我的一些类(class),但是当我使用 NSubstitute 检查收到的电话时,它说没有收到匹配的电话。 我正在尝试测试 Tick()
我正在使用 Nsubstitute 进行模拟。为了减少代码,我想编写一个伪造通用属性的通用类: public class Tester where TValue: IValue { /
我是一名优秀的程序员,十分优秀!