我有一个在 MVC2 中运行良好的单元测试。该测试简单地定义 Controller 上的操作、必要的 stub ,并测试 View 的名称。但是,升级到 MVC3 后,当我调用该方法时,出现上述错误。站点 MVC3 升级工作正常;由于升级,我只是让这些单元测试失败了。谢谢。
这是我的操作:
public partial class GadgetController
{
[SetterProperty]
public IATCGadgetProxy ATCGadgetService { get; set; }
public ActionResult LoadForums(bool popularOnly, bool myThreads, int itemCount)
{
var model = ATCGadgetService.LoadForums(popularOnly, myThreads, itemCount);
return View("AskTheCommunity-Forums", model);
}
}
这是测试。从操作返回 View 时失败。
[TestMethod]
public void Test_Forums_Action_Type()
{
GadgetController controller = new GadgetController();
controller.ATCGadgetService = new ATCGadgetServiceStub();
ViewResult result = controller.LoadForums(false, false, 10) as ViewResult;
Assert.IsNotNull(result);
Assert.AreEqual("AskTheCommunity-Forums", result.ViewName);
}
最佳答案
我知道这是一个旧线程,但我在 MVC 5.2.3 中遇到了同样的错误...但使用了 Xunit。最后,这并不重要,因为解决问题的方法是一样的。
首先,您必须确保 MVC 已添加到您的测试项目中。我通过 NuGet 添加了它:
Install-Package Microsoft.AspNet.Mvc -Version 5.2.3
或者您可以将版本更改为您正在使用的任何 MVC 版本。
然后,我仍然有错误。我刚刚发现 App.config 页面缺少信息。一旦我确定我有这些行,一切正常:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- Other config here -->
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
关于c# - ASP.NET MVC3 升级 - "Attempt by method [whatever] to access method System.Web.Mvc.Controller.View(...) failed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6698518/