How to check that Asp.Net MVC Views are displayed without exception?

A small problem appeared in my application.

I changed the namespace of my main application on MyApp.Models to MyApp.ViewModels , so the namespaces were less confused as a whole (since this namespace only has view models and business models). I changed all the links that I could find, including my views, and I ran all my unit tests again and went through the application, and everything seemed fine.

A few days later I received a message that an error occurred on the registration page when it was raised. Having looked, it turns out, I forgot to fix the namespace on the registration page, and thus, he could not compile the view.

It bothers me. The whole purpose of unit testing and one of the biggest Asp.Net MVC decoys is to have confidence in your application, individually checking everything individually so you know immediately when your modification breaks down parts of the system. Views now seem like a big hole in that.

To be clear, I know that you can enable precompilation of views. However, I don't like this option, since it is not a good idea to have it all the time (since it makes assembling a new assembly very, very slow), and having a separate configuration scheme for this, it means that up to the user don't forget to try to compile this configuration scheme to check for compilation errors.

It also completely bypasses checking for runtime errors that may occur. For example, let's say you change the view model that a strongly typed view expects. Then you update the unit test to make sure that Controller.Action() returns the result of the view with the correct view model type, but it does nothing to make sure that the actual view has been updated correctly for the new view, and yet this script will result in an exception at runtime. This is a simple scenario, because if the differences in the two view models are only visible in the partial ones used in the view.

Other examples of code that may throw runtime exceptions in views are incorrect loops (possibly caused by changes in the view model), code that checks the user's role (therefore, buttons are only displayed for users with credentials), incorrect clicks (for example, to convert the collection to a selection list) the wrong code to sort the collection (as sorting the collection on the display can be seen as a presentation problem, not a problem with the controller or view), if the lines used to place the files, t works correctly (T4MVC does not integrate very well with some things, such as the Telerik script registration system), etc.

As I see this, there are many things that can throw an exception during the rendering of a view, and I cannot find a way to create unit or integration tests to determine when this will happen. I would feel more comfortable if I didn't have to check every page to make sure I missed the compilation time or runtime for something that could be caught by the standard unit test.

What options do I need to do?

I would prefer to stay away from WaTiN and other GUI testing tools, because for this I am not interested in the actual display of the page, I only want to know if a view is raised or an exception is raised, and I do not need the Watin overhead running an IE instance for each test (I also think that this will cause problems if I continue the integration at a later point).

+6
source share
2 answers

If you do not want to use WaTIN and IE, how about running your website in IIS Express, and then using the HttpWebRequest for each of your views to check the result, 200 OK. This is a complete integration test.

Otherwise, you should get the ViewResult from your controller and call the ExecuteResult method passing in the ControllerContext containing the wrapped HttpContextBase . This gives more true unit test, and will be faster, but you have a lot of ridicule and stubbing before it works.

+3
source

Source: https://habr.com/ru/post/888254/


All Articles