views:

174

answers:

1

Hi I am finding that a common breakage point in our mvc setup is when the controller returns

return View();

or whatever, and the viewengine cannot find the view.

I would like to be able to test my controllers and then run ExecuteResult on the resulting ViewResult but the underlying ViewEngine is so tied to virtual directories that I can't figure out how to test it.

Ideally I would actually like to be able to render the view as well as determining if it has been found.

I can't find too much about this on the net and I am wondering just how 'testable' asp.net mvc actually is :(

+1  A: 

I think your problem is that you are trying to test too much.

Without seeing the code its a bit difficult but some initial starting points:

1) Decouple your controller from your virtual directories - the controller should be isolated and not depend on the underlying web servers setup. What are you accessing via this?

Your controller will return a ViewResult, this should have your model attached which you can then use to verify the correct data is being passed to your View. Using stubs\mocks and dependency injection to verify the controller is communcating correctly to the underlying services such as your database Repository.

Have a separate set of integration tests to cover your repositories implementation.

2) Use a web testing framework such as Watin to verify the view renders as expected. This will allow you to interact with the webpage and the HTML dom to verify it works as expected. Test your controllers via unit tests. Two separate tests for two different layers.

The aim of unit testing is to keep your tests and code as isolated as possible.

Ben Hall