Ninject:使用NSubstitute自动模拟吗?

编程入门 行业动态 更新时间:2024-10-19 07:28:26
本文介绍了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自动模拟吗?

本文发布于:2023-11-16 11:36:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603925.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Ninject   NSubstitute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!