29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Figure 17-3. IntelliSense support for strongly typed views<br />

UNIT TEST: VIEW MODEL OBJECTS<br />

You can access the view model object passed from the action method to the view through the<br />

ViewResult.ViewData.Model property. Here is the test for the action method in Listing 17-17. You can see<br />

that I have used the Assert.IsInstanceOfType method to check that the view model object is an instance of<br />

DateTime:<br />

...<br />

[TestMethod]<br />

public void ViewSelectionTest() {<br />

// Arrange - create the controller<br />

ExampleController target = new ExampleController();<br />

// Act - call the action method<br />

ViewResult result = target.Index();<br />

// Assert - check the result<br />

Assert.AreEqual("", result.ViewName);<br />

Assert.IsInstanceOfType(result.ViewData.Model, typeof(System.DateTime));<br />

}<br />

...<br />

I had to change the name of the view that I check for to reflect the changes in the action method since the last unit test I<br />

showed you, as follows:<br />

...<br />

[TestMethod]<br />

public void ControllerTest() {<br />

451

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!