本文介绍了Ninject:使用NSubstitute自动模拟吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以帮忙,我在使用Ninject和NSubstitute之间可用的自动模拟时遇到问题,实际上该程序包是一个名为Ninject.MockingKernel.NSubstitute的ninject打包程序,它应该允许我使用Ninject创建模拟并返回带有模拟的实例注射.
Can anyone help, I am having problems using the auto-mocking that is available between Ninject and NSubstitute, actually the package is a ninject packaged called Ninject.MockingKernel.NSubstitute which should allow me to use Ninject to create mocks and return instances with mocks injected.
Moq和Rhinomocks似乎有一些例子,但NSubstitute却没有.
There seems to be a few examples for Moq and Rhinomocks but I don't see any for NSubstitute.
到目前为止,我是
this.kernel = new NSubstituteMockingKernel(); var summaryService = this.kernel.GetMock<IMyService>(); // GetMock not available有人用吗?
推荐答案以下是从源代码:
[TestFixture] public class Tests { /// <summary> /// Mocks are singletons. /// </summary> [Test] public void MocksAreSingletons() { using (var kernel = new NSubstituteMockingKernel()) { var firstReference = kernel.Get<IDummyService>(); var secondReference = kernel.Get<IDummyService>(); firstReference.Should().BeSameAs(secondReference); } } /// <summary> /// Real objects are created for auto bindable types. /// </summary> [Test] public void RealObjectsAreCreatedForAutoBindableTypes() { using (var kernel = new NSubstituteMockingKernel()) { var instance = kernel.Get<DummyClass>(); instance.Should().NotBeNull(); } } /// <summary> /// Reals objects are singletons. /// </summary> [Test] public void RealObjectsAreSingletons() { using (var kernel = new NSubstituteMockingKernel()) { var instance1 = kernel.Get<DummyClass>(); var instance2 = kernel.Get<DummyClass>(); instance1.Should().BeSameAs(instance2); } } /// <summary> /// The injected dependencies are actually mocks. /// </summary> [Test] public void TheInjectedDependenciesAreMocks() { using (var kernel = new NSubstituteMockingKernel()) { var instance = kernel.Get<DummyClass>(); instance.DummyService.Do(); instance.DummyService.Received().Do(); } } public interface IDummyService { void Do(); } public class DummyClass { public DummyClass(IDummyService dummyService) { this.DummyService = dummyService; } public IDummyService DummyService { get; set; } } }更多推荐
Ninject:使用NSubstitute自动模拟吗?
发布评论