views:

48

answers:

1

I'm migrating a webforms application written w/ MVP to leverage the MVC framework and wanted to know how I should be testing a simple controller action that returns a partialview. When using MVP i would assert true for each property on the view = the expected value.

Controller action under test

<OutputCache(Location:=OutputCacheLocation.None)> _
Function Edit(ByVal id As Integer) As ActionResult
    Dim Form As Form = mFormService.GetFormById(id)

    Return PartialView("Form", Form)
End Function

What I want to verify is the "Form" values show up correctly in the view (but is this what I should be testing as I did when using the MVP pattern?)

+3  A: 

You should be testing the ViewModel and the data residing therein. In addition, you should test if the result is the type of result you expected (e.g. ViewResult in most cases). Lastly, you should check the view name.

For example, this is a test for a controller action that returns an a create form for adding a new User entity to an existing Company. The Company's ID is supplied to the controller as id parameter:

        [Test]
        public void Create_Get_Shows_View()
        {
            //Setup    
            //setup Controller, fakes, mocks, etc... here

            //Execution
            var result = (ViewResult)Controller.Create(companyID);

            //Assertion
            var model = (UsersController.CreateViewModel) result.ViewData.Model;
            Assert.AreEqual("", result.ViewName);
            Assert.IsNotNull(model.User);
            Assert.AreEqual(companyID,model.CompanyID);
        }

Edit: You might also want to test whether the proper HttpVerb is set. You can do this via reflection.

Adrian Grigore