2011-07-14 48 views
6

我有一個在MVC2中正常工作的單元測試。測試只是在控制器上定義了Action,必要的存根,並測試了視圖的名稱。但是,在升級到MVC3之後,當我調用該方法時,出現上述錯誤。網站MVC3升級工作得很好;我只是因爲升級而導致這些單元測試失敗。謝謝。ASP.NET MVC3升級 - 「嘗試訪問方法System.Web.Mvc.Controller.View(...)方法[無論]失敗」

這裏是我的行動:

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); 
    } 
} 

這裏的測試。它從Action返回視圖時失敗。

[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); 
} 
+1

請顯示您要測試的代碼以及您的測試。在目前的狀態下,你的問題很難回答。 –

+0

上面的代碼。謝謝 –

+0

我有同樣的問題 –

回答

3

讓我瘋狂地將項目升級到MVC 3的原因之一是這些奇怪的,無法解釋的錯誤。直到我發現並非所有的項目都升級到了MVC 3(在你的情況下 - 這可能是測試項目),並保留在MVC 2中,這導致了一些非常奇怪的行爲,比如你所描述的行爲。請檢查您的測試項目中的System.Web.Mvc版本(以及可能相關的程序集)。

+0

@Ryan幫你嗎?你沒有給出任何反饋。 –

+1

當我編譯一個引用MVC 4的項目,然後部署到擁有MVC 3的網站時,情況就是這樣。我改變了對MVC 3的引用,並開始工作。謝謝! –

4

我知道這是一箇舊的線程,但我剛剛得到與MVC 5.2.3相同的錯誤...但使用Xunit。最終,解決問題的方式是一樣的,這並不重要。

首先,您必須確保MVC已添加到您的測試項目中。我通過添加的NuGet它:

安裝,包裝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> 
相關問題