4.2. Design

Let's start with the test code. The assert statements can be grouped into several extension methods that extend the appropriate object. For example, asserting the result of actions that redirect to a different URL can be grouped together into an extension method on the ActionResult class.

Several of the tests started off with the same set of statements that created the mocks and initialized the controller with the mocked objects. These lines can also be refactored into a central location for reuse by all tests. All mocked objects can be moved to a static class that can then be accessed by all tests.

Now, on the code side, I wanted to use a strongly typed class as the parameter to my actions instead of using multiple parameters that just resulted in unnecessary typing. This can be achieved by using Model Binders to tell the framework how to convert the form's values posted into a strongly typed object. For example, take a look at the LoginModel class in Figure 4-1, which can replace the Username, Password, and RememberMe parameters.

Figure 4-1. Figure 4-1

Get ASP.NET MVC 1.0 Test Driven Development: Problem - Design - Solution now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.