开发者

How to mock ASP.NET 3.5 to unit test my web controls?

开发者 https://www.devze.com 2022-12-19 06:40 出处:网络
I want to mock ASP.NET 3.5 behavior in order to unit test my WebControls: I want to test how they perform with mock data with existing system of events. Basically I want to test generated result HTML

I want to mock ASP.NET 3.5 behavior in order to unit test my WebControls: I want to test how they perform with mock data with existing system of events. Basically I want to test generated result HTML based on input mock data.

How to do it?

I looked into NMock, but it doesn't suit my needs for 2 reasons:

  1. It just runs ASP.NET server in separate process in order to get resulting HTML;
  2. My WebControls output HTML as a text on render, so there are elements which do not correspond to any ASP.NET control (except LiteralControl in some cases).

Also, I have huge legacy system, so rewrit开发者_JAVA技巧ing everything for MVC won't work for me (too much job), I need to write unit tests to existing controls.


Unit testing is really ideal for middle-tier and back-end code which can be called without regard to the environment or context. The more you can separate out your code and build environmental/contextual requirements into parameters the easier and more reliable it is to test.

Unit testing UI code is not as straightforward. Typically the best thing is to separate the pure UI code (html generation in this case) from the UI-related logic code into a View and ViewModel classes. However, you said you have a large codebase and can't make major changes.

Therefore it's probably appropriate to look at the next step beyond unit testing. Use a UI-based regression testing tool to do UI automation and verification.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号