c# - 使用 Rhino 模拟模拟 ASPNET MVC DependencyResolver

标签 c# asp.net asp.net-mvc unit-testing rhino-mocks

在为其中一个业务逻辑(方法)编写单元测试时,无法模拟 ASP NET MVC DependencyResolver,因为它会为此获取 null。下面是这行代码

DependencyResolver.Current.GetService(typeof(ITestDetails)) as ITestDetails;

不知何故想 mock 这一行。

最佳答案

模拟解析器及其预期行为

//create the new resolver that will be used to replace the current one
IDependencyResolver resolver = MockRepository.GenerateMock<IDependencyResolver>();
//mock expected behavior
var testdetails = MockRepository.GenerateMock<ITestDetails>();
resolver.Stub(_ => _.GetService(typeof(ITestDetails))).Returns(testDetails);

并将当前设置为模拟。

//assign the mocked resolver.
DependencyResolver.SetResolver(resolver);

现在当

DependencyResolver.Current.GetService(typeof(ITestDetails))

被调用,它将提供模拟的解析器并在单元测试时按预期运行

关于c# - 使用 Rhino 模拟模拟 ASPNET MVC DependencyResolver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56473634/

相关文章:

asp.net - 为什么我在 Visual Studio 2017 中收到 "Visual Basic 12 does not support multiline string literals"?

asp.net - CSS,ASP :listview and nth-child

c# - 将 AjaxOnlyAttribute 和 ChildActionOnlyAttribute 合并到一个 Action 过滤器中

c# - VSTO Outlook 2010 C# Active Explorer 关闭

javascript - 检查外部 Javascript 文件中的 IsPostBack

c# - c++ exe如何定位c# dll?

html - @Html.ActionLink 和 Angularjs 值?

asp.net-mvc - edmx 重建会创建一个重复的设计器 cs 文件

c# - 将 HttpRequestMessage 转换为 OwinRequest 并将 OwinResponse 转换为 HttpResponseMessage

c# - 如何返回异步 IEnumerable<string>?