Asp.net mvc unit testing - Can the preview be previewed?
I find that the common breakpoint in our mvc setup is when the controller returns
return View();
or whatever and the viewview cannot find the view.
I would like to be able to test my controllers and then run ExecuteResult on the resulting ViewResult, but the main ViewEngine is bound to virtual directories and I can't figure out how to test it.
Ideally I would like to be able to render the view and also determine if it was found.
I can't find too much about this on the net and I'm wondering how the "testable" asp.net mvc actually is.
I think your problem is that you are trying to check too much.
Without looking at the code, it's a little tricky, but some initial starting points:
1) Disconnect the controller from your virtual directories - the controller should be isolated and independent of the configuration of the underlying web servers. What do you get through this?
The controller will return a ViewResult, this should have your model attached, which you can use to check if the data is passed to your view correctly. Using stubs \ mocks and injection injection to verify that the correct connection to basic services such as the database repository is correct.
You have a separate set of integration tests to implement your repositories.
2) Use a web testing framework like Watin to test the rendering of the view as expected. This will allow you to interact with the web page and HTML dom to check if it works as expected. Test your controllers with unit tests. Two separate tests for two different layers.
The goal of unit testing is to keep your tests and code as isolated as possible.
a source to share