我想模拟(使用MOQ)由Findview方法返回的视图,如下所示,这样视图就不是空的:
if ((ViewEngines.Engines.FindView(ControllerContext, viewName, masterName)).View == null)
{
viewName = SomeViewName;
}我模仿过这样的viewengine,例如在线:
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);但是当我运行test时,它给出了这个错误:
System.NullReferenceException : Object reference not set to an instance of an object.
at System.Web.Mvc.ViewEngineCollection.Find(Func`2 cacheLocator, Func`2 locator)发布于 2012-05-08 17:50:51
在设置FindView方法时,应该使用It.IsAny < bool >而不只是false。
https://stackoverflow.com/questions/5227070
复制相似问题