如何测试Asp.Net MVC视图是否无异常地呈现?

时间:2021-03-20 04:12:12

I have come up with a small issue in my application.

我在我的申请中提出了一个小问题。

I changed the namespace of my main application to be from MyApp.Models to MyApp.ViewModels so that the namespaces were less confusing overall (since that namespace only has view models and no business models). I changed all the references I could find, including in my views, and I re-ran all my unit tests and went through the application, and all seemed fine.

我将主应用程序的命名空间从MyApp.Models更改为MyApp.ViewModels,以便命名空间不那么混乱(因为该命名空间只有视图模型而没有业务模型)。我更改了所有可以找到的引用,包括在我的视图中,我重新运行了所有的单元测试并完成了应用程序,一切似乎都很好。

A few days later I got a report that the registration page had an error when it was brought up. After looking it turns out I forgot to fix the namespace on the registration page, and thus it was failing to compile the view.

几天后,我收到一份报告,说明注册页面出现时出错了。看完之后,我忘记在注册页面上修复命名空间,因此无法编译视图。

This worries me. The whole point of unit testing, and one of the biggest lures of Asp.Net MVC, is to be able to have confidence in your application by having everything tested individually automatically so that you know immediately when your modification breaks parts of the system. Views right now seem like a major hole in this.

这让我担心。单元测试的全部要点,以及Asp.Net MVC最大的诱惑之一,就是能够通过自动单独测试所有内容来确保您的应用程序,以便您在修改时立即知道系统的某些部分。现在看来,这似乎是一个重大漏洞。

To be clear, I am aware you are able to turn on pre-compiling a views. However, I do not like this option as it's not a good idea to have this on all the time (as it makes compiling a new build very, very slow), and having a separate configuration scheme for this means it's up to the user to remember to attempt to compile with that configuration scheme to check for view compile errors.

要清楚,我知道您可以打开预编译视图。但是,我不喜欢这个选项,因为它一直都不是一个好主意(因为它使得编译新的构建非常非常慢),并且具有单独的配置方案,这意味着它取决于用户记得尝试使用该配置方案进行编译以检查视图编译错误。

This also completely bypasses checking for runtime errors that may occur. For example, lets say you change the viewmodel that a strongly-typed view is expecting. You would then update the unit test to make sure that the Controller.Action() is returning a view result with the correct view model type, but that does nothing to make sure that the actual view was updated correctly for the new view, and yet this scenario will cause a run-time exception. This is an easy scenario to occur, especially since if the differences in the two view models are only seen in partials used within the view.

这也完全绕过了检查可能发生的运行时错误。例如,假设您更改了强类型视图所期望的视图模型。然后,您将更新单元测试以确保Controller.Action()返回具有正确视图模型类型的视图结果,但这不会确保为新视图正确更新实际视图,此方案将导致运行时异常。这是一个容易发生的情况,特别是因为如果两个视图模型中的差异仅在视图中使用的部分中看到。

Other examples of code that can cause runtime exceptions in views are incorrect loops (possibly caused by changes in a view model), code that checks a user's role (so buttons are only shown for users with credentials), incorrect casts (e.g. for converting a collection into a select list), incorrect code to sort a collection (how a collection is sorted in the display can construed as a view concern, not a controller or view model concern), if strings used for locations of files don't work correctly (T4MVC doesn't integrate well with some things, such as Telerik's script registration system) etc...

可能导致视图中的运行时异常的代码的其他示例是不正确的循环(可能由视图模型中的更改引起),检查用户角色的代码(因此仅对具有凭据的用户显示按钮),不正确的强制转换(例如,用于转换收集到选择列表中),对集合进行排序的错误代码(如何在显示中对集合进行排序可视为视图问题,而不是控制器或视图模型问题),如果用于文件位置的字符串无法正常工作(T4MVC与某些东西不能很好地集成,例如Telerik的脚本注册系统)等等......

The way I see it, there are many things that can cause an exception to occur in the process of rendering a view, and I can't seem to find any way to create unit or integration tests to determine when these occur. I would feel more comfortable if I did not have to check every page to make sure I missed a compile time or runtime error for something that a standard unit test should be able to catch.

我看到它的方式,有许多事情可以导致在渲染视图的过程中发生异常,我似乎无法找到任何方法来创建单元或集成测试来确定何时发生。如果我不必检查每一页以确保我错过了标准单元测试应该能够捕获的内容的编译时间或运行时错误,我会感觉更舒服。

What options do I have to do this?

我有什么选择呢?

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

我宁愿远离WaTiN和其他GUI测试工具,因为我对页面的实际显示不感兴趣,我只想知道视图是否呈现或是否发生异常并且不需要开销Watin为每个测试运行一个IE实例(我也认为如果我以后继续进行集成会导致问题)。

2 个解决方案

#1


3  

If you don't want to use WaTIN and IE, how about firing up your web site in IIS Express, and then using HttpWebRequest over each of your Views' urls to check the result is 200 OK. This is a complete integration test.

如果您不想使用WaTIN和IE,那么如何在IIS Express中启动您的网站,然后在每个视图的网址上使用HttpWebRequest来检查结果是200 OK。这是一个完整的集成测试。

Otherwise, you have to get the ViewResult from your controller, and call the ExecuteResult method passing in a ControllerContext containing a stubbed HttpContextBase. This gives more of a true unit test, and would be faster, but you've got a lot of mocking and stubbing to do before it will work.

否则,您必须从控制器获取ViewResult,并调用ExecuteResult方法传入包含存根HttpContextBase的ControllerContext。这提供了更多真正的单元测试,并且会更快,但是在它工作之前你需要做很多嘲弄和捣乱。

#2


1  

mspec mvc extensions

mspec mvc扩展

#1


3  

If you don't want to use WaTIN and IE, how about firing up your web site in IIS Express, and then using HttpWebRequest over each of your Views' urls to check the result is 200 OK. This is a complete integration test.

如果您不想使用WaTIN和IE,那么如何在IIS Express中启动您的网站,然后在每个视图的网址上使用HttpWebRequest来检查结果是200 OK。这是一个完整的集成测试。

Otherwise, you have to get the ViewResult from your controller, and call the ExecuteResult method passing in a ControllerContext containing a stubbed HttpContextBase. This gives more of a true unit test, and would be faster, but you've got a lot of mocking and stubbing to do before it will work.

否则,您必须从控制器获取ViewResult,并调用ExecuteResult方法传入包含存根HttpContextBase的ControllerContext。这提供了更多真正的单元测试,并且会更快,但是在它工作之前你需要做很多嘲弄和捣乱。

#2


1  

mspec mvc extensions

mspec mvc扩展