Mock view returned by FindView
I want to mock (using MOQ) view returned by Findview method as shown below, so that view is not null:
if ((ViewEngines.Engines.FindView(ControllerContext, viewName, masterName)).View == null)
{
viewName = SomeViewName;
}
i have mocked viewengine like this following an e.g. online:
var mockViewEngine 开发者_StackOverflow社区= 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);
but when i run test it gives me this error:
System.NullReferenceException : Object reference not set to an instance of an object.
at System.Web.Mvc.ViewEngineCollection.Find(Func`2 cacheLocator, Func`2 locator)
You should use It.IsAny < bool > instead of just false when setup FindView method.
精彩评论