HI there
I was wondering if there is a better way of testing that a view has rendered in MVC.
I was thinking perhaps I should render the view to a string but perhaps there are simpler methods?
Basically what I want to know if that the view for a given action has rendered without errors I m already testing the view model but I want to see that rendering the view giving a correct ViewData.Model works
Use the MvcContrib TestHelpers library to perform assertions that a particular view is being returned from your action:
var sampleController = new SampleController();
sampleController.New().AssertViewRendered().ForView("New").WithViewData<SomeModel>();
To make assertions to ensure you are returning the correct data to the view, pull the model from the ActionResult
:
var result = (ViewResult)sampleController.New();
((SomeModel)result.ViewData.Model).SomeProperty.ShouldNotBeNull();
This is as far as your unit testing should go.
For end-to-end automated functional/GUI testing you might want to think about using a tool like Selenium.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With