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\dmarshall\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 = 新しい 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 に答える