Tag: automocking

Ninject:使用NSubstitute进行自动模拟?

任何人都可以提供帮助,我在使用Ninject和NSubstitute之间可用的自动模拟时遇到问题,实际上这个包是一个名为Ninject.MockingKernel.NSubstitute的ninject包,它应该允许我使用Ninject创建Ninject.MockingKernel.NSubstitute并返回实例注入Ninject.MockingKernel.NSubstitute 。 Moq和Rhinomocks似乎有一些例子,但我没有看到NSubstitute。 到目前为止我所拥有的是什么 this.kernel = new NSubstituteMockingKernel(); var summaryService = this.kernel.GetMock(); // GetMock not available 有人用吗?

如何使用AutofacContrib.NSubstitute监视测试中的类

我正在使用NSpec框架,AutofacContrib.NSubstitute v3.3.2.0,NSubstitute v1.7.0.0(最新版本为1.8.2)的类库项目中运行unit testing。 Class Under Test实例是使用AutoSubstitute ,以便自动锁定所有需要的依赖项。 AutoSubstitute autoSubstitute = new AutoSubstitute(); MainPanelViewModel viewModel = autoSubstitute.Resolve(); 如果工作正常,我的Class Under Test会在某个时候调用其中一个基类方法,并带有一些特定的输入参数(基类不在我的控制范围内): // … base.ActivateItem(nextScreen); // … 因此,对于测试期望,我需要检查(间谍)实例调用基本方法: viewModel.Received().ActivateItem(Arg.Any()); 这是问题:当我尝试这样做时,在运行时NSubstitute抱怨我只能对使用Substitute.For()创建的对象运行Received() Substitute.For() 。 我还快速检查了AutofacContrib.NSubstitute源代码,但我找不到一种方法来获取具有自动锁定的实例,同时将它以某种方式包装在间谍对象或类似的东西中。 我也认为也许Substitute.ForPartsOf()可能会有所帮助,但在NSubstitute v1.7.0中似乎找不到该方法。 为了完整起见,这里的NSubstitute完全错误: 像.Received()这样的NSubstitute扩展方法只能在使用Substitute.For()和相关方法创建的对象上调用。

AutoFixture作为Automocking容器与Automocking差异?

我开始使用moq但是根据我的理解,我总是要模拟所有可以调用的方法,即使我真的不关心它们。 有时需要很长时间来模拟你忘记你想做什么的东西。 所以我一直在看自动模拟,但我不确定我应该使用什么。 AutoFixture作为自动模拟容器 Automocking 我根本不知道如何使用第一个。 我有点得到第二个但从未真正尝试过。 我不确定一个人是否比另一个好。 我唯一知道的是我使用AutoFixtures已经是第一个的依赖。 所以也许从长远来看,与第一个一起使用是有意义的,但就像我说我找不到任何关于如何使用它的基本教程。 编辑 我试图遵循“Nikos Baxevanis”的例子,但我遇到了错误。 Failure: System.ArgumentException : A matching constructor for the given arguments was not found on the mocked type. —-> System.MissingMethodException : Constructor on type ‘DatabaseProxyded46c36c8524889972231ef23659a72’ not found. var fixture = new Fixture().Customize(new AutoMoqCustomization()); var fooMock = fixture.Freeze<Mock>(); // fooMock.Setup(x => x.GetAccounts(It.IsAny())); var sut […]

AutoFixture.AutoMoq为一个构造函数参数提供已知值

我刚开始在我的unit testing中使用AutoFixture.AutoMoq ,我发现它对于创建我不关心特定值的对象非常有帮助。 毕竟,匿名对象创建就是它的全部。 我正在努力的是当我关心一个或多个构造函数参数时。 以下面的ExampleComponent : public class ExampleComponent { public ExampleComponent(IService service, string someValue) { } } 我想编写一个测试,其中我为someValue提供了一个特定的值,但是由AutoFixture.AutoMoq自动创建IService 。 我知道如何在我的IFixture上使用Freeze来保持一个已注入组件的已知值,但我不太明白如何提供我自己的已知值。 这是我理想的做法: [TestMethod] public void Create_ExampleComponent_With_Known_SomeValue() { // create a fixture that supports automocking IFixture fixture = new Fixture().Customize(new AutoMoqCustomization()); // supply a known value for someValue (this method doesn’t exist) string knownValue = […]