首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FindView返回的模拟视图

FindView返回的模拟视图
EN

Stack Overflow用户
提问于 2011-03-08 08:42:44
回答 1查看 1.1K关注 0票数 1

我想模拟(使用MOQ)由Findview方法返回的视图,如下所示,这样视图就不是空的:

代码语言:javascript
复制
if ((ViewEngines.Engines.FindView(ControllerContext, viewName, masterName)).View == null)
            {
                viewName = SomeViewName;
            }

我模仿过这样的viewengine,例如在线:

代码语言:javascript
复制
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时,它给出了这个错误:

代码语言:javascript
复制
System.NullReferenceException : Object reference not set to an instance of an object.
at System.Web.Mvc.ViewEngineCollection.Find(Func`2 cacheLocator, Func`2 locator)
EN

回答 1

Stack Overflow用户

发布于 2012-05-08 17:50:51

在设置FindView方法时,应该使用It.IsAny < bool >而不只是false。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5227070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档