开发者

Unit testing ASP.NET Code behind

I've been reading about MVC in which the authors suggest that testability is one of the major strengths of MVC. They go to compare it with ASP.NET WebForms and how difficult it is to test the code behind in WebForms.

I do underst开发者_开发知识库and it's difficult but can someone explain how unit tests were written to test code behind logic in the old days?


In the old days I tested aspnet webforms using the pattern Model View Presenter. I was able to test code with this pattern because I abstracted the conditional logic / loops / etc into a separate class that didn't live inside the webforms framework.

What was left in the webforms codebehind was nothing more than a few properties and a call in the page load to init the presenter class itself.

Then each event handler would simply pass off the work to the presenter class.

I've spent a great deal of time with this pattern and found that it makes things much more test friendly but it's a great deal of work compared to aspnet mvc


The code behind are plain methods in a class (the only difference with another class is that this class inherits from the Page object)

So it is testable. most problems arise because the methods were tightly coupled to web.ui controls like the grid; they were not so easy to fake. If you didn't fake the UI controls, you were also testing the inner workings of the UI controls which is a little overdone.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜