我想模拟(使用MOQ)Findview方法返回的视图,如下所示,这样视图不为null:
if ((ViewEngines.Engines.FindView(ControllerContext, viewName, masterName)).View == null)
{
viewName = SomeViewName;
}
我已经嘲笑了像这样的视频引擎在线:
var mockViewEngine = new Mock<IViewEngine>();
// Depending on what result you expect you could set the searched locations
// and the view if you want it to be found
Mock<IView> view = new Mock<IView>();
var result = new ViewEngineResult(new[] { "location1", "location2" });
// Stub the FindView method
mockViewEngine
.Setup(x => x.FindView(It.IsAny<ControllerContext>(), It.IsAny<string>(), It.IsAny<string>(), false))
.Returns(result);
// Use the mocked view engine instead of WebForms
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(mockViewEngine.Object);
但是当我运行测试时,它给了我这个错误:
System.NullReferenceException : Object reference not set to an instance of an object.
at System.Web.Mvc.ViewEngineCollection.Find(Func`2 cacheLocator, Func`2 locator)
答案 0 :(得分:0)
你应该使用It.IsAny&lt; bool&gt;设置FindView方法时,而不是只是假。