views:

194

answers:

1

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 rewriting everything for MVC won't work for me (too much job), I need to write unit tests to existing controls.

+1  A: 

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.

Sam
Ended up with refactoring to do code more testable.
Artem