我正在尝试对我编写的自定义 View 引擎进行单元测试。
View 引擎的预期功能是,在执行FindView时,它会更改基本RazorViewEngine的外观。
这是我的单元测试
public void ViewEngineReturnsDependencyView()
{
//Mock http request
var mockRequest = new Mock<HttpRequestBase>();
//Mock server variable
NameValueCollection variables = new NameValueCollection();
variables.Add("APPL_PHYSICAL_PATH", TEST_APPLICATION_PATH);
mockRequest.Setup(r => r.ServerVariables).Returns(variables);
//Mock http context
var mockHttpContext = new Mock<HttpContextBase>();
//Mock route
mockHttpContext.Setup(c => c.Request).Returns(mockRequest.Object);
var routeData = new RouteData();
routeData.Values.Add("controller", "testController");
routeData.Values.Add("action", "testAction");
//Mock controller context
var controllerContext = new testController().ControllerContext;
controllerContext.HttpContext = mockHttpContext.Object;
controllerContext.RouteData = routeData;
var mockControllerContext = new ControllerContext(mockHttpContext.Object,
routeData,
new Mock<ControllerBase>().Object);
//Run find view
viewEngine.FindView(mockControllerContext, "TestView", null, false);
}
令人讨厌的viewEngine.FindView(...);
引发异常:我的问题是,如何创建适当的模拟以对RazorViewEngine.FindView()进行单元测试?
最佳答案
System.Web.WebPages.DisplayModeProvider.GetDisplayMode()
方法正在使用HttpContext.Items
属性,您还需要模拟该属性。
尝试:
mockHttpContext.Setup(c => c.Items).Returns(new Dictionary<object, object>());
关于c# - 单元测试自定义ViewEngine,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19556203/