views:

94

answers:

1

I'm wring a unit test for a controller and here is my code.

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

I'm basically following along with Rob Conery's asp.net storefront application and realized that I can't use the RenderView method. As shown I have tried the ViewResult method to create an instance of the view. I'm getting this error: Error 1 Cannot convert type 'System.Web.Mvc.ViewDataDictionary' to 'HomeOwners.Controllers.DocumentsController.DocumentsData' C:\Documents and Settings\drmarshall\My Documents\Visual Studio 2008\Projects\HomeOwners\HomeOwners.Tests\DocumentsControllerTests.cs 61 54 HomeOwners.Tests

Am I using the correct replacement method or am I missing something?

I figured it out.

[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);
    }
A: 

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