2009-02-04 132 views
0

我正在爲控制器進行單元測試,這是我的代碼。ASP.net MVC測試版Beta 1

public void DocumentController_IndexMethod_ShouldReturn_Documents() 
    { 
     DocumentsController c = new DocumentsController(_repository); 

     ViewResult result = (ViewResult)c.Index("1"); 

     DocumentsController.DocumentsData data = (DocumentsController.DocumentsData)result.ViewData; 

     Assert.IsNotNull(data.Documents); 
     Assert.IsTrue(data.Documents.Count() > 0); 

     Assert.IsNotNull(result); 
    } 

我基本上跟隨着Rob Conery的asp.net店面應用程序,並意識到我不能使用RenderView方法。如圖所示,我嘗試了ViewResult方法來創建視圖的一個實例。我收到此錯誤: 錯誤1無法將類型'System.Web.Mvc.ViewDataDictionary'轉換爲'HomeOwners.Controllers.DocumentsController.DocumentsData'C:\ Documents and Settings \ drmarshall \ My Documents \ Visual Studio 2008 \ Projects \ HomeOwners \ HomeOwners.Tests \ DocumentsControllerTests.cs 61 54 HomeOwners.Tests

我是使用正確的替換方法,還是我錯過了什麼?

我想通了。

[TestMethod] 
    public void DocumentController_IndexMethod_ShouldReturn_Documents() 
    { 
     DocumentsController c = new DocumentsController(_repository); 

     ViewResult result = c.Index("1") as ViewResult; 

     ViewDataDictionary dictionary = result.ViewData; 

     DocumentsController.DocumentsData data = (DocumentsController.DocumentsData)dictionary["Documents"]; 

     Assert.IsNotNull(data.Documents); 
     Assert.IsTrue(data.Documents.Count() > 0); 

     Assert.IsNotNull(result); 
    } 
+0

如果在這裏有一個問題,我不能告訴它是什麼。 – 2009-02-04 16:53:52

回答

0

[TestMethod的] 公共無效DocumentController_IndexMethod_ShouldReturn_Documents() { DocumentsController C =新DocumentsController(_repository);

ViewResult result = c.Index("1") as ViewResult; 

    ViewDataDictionary dictionary = result.ViewData; 

    DocumentsController.DocumentsData data = (DocumentsController.DocumentsData)dictionary["Documents"]; 

    Assert.IsNotNull(data.Documents); 
    Assert.IsTrue(data.Documents.Count() > 0); 

    Assert.IsNotNull(result); 
}