0

我正在为控制器编写单元测试,这是我的代码。

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

1 回答 1

0

[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);
}
于 2010-02-04T15:27:00.320 回答