Monday, July 06, 2009

An Opinion on Repository

First, what is a repository?

…Therefore, use a Repository, the purpose of which is to
encapsulate all the logic needed to obtain object references. The
domain objects won’t have to deal with the infrastructure to get
the needed references to other objects of the domain. They will
just get them from the Repository and the model is regaining its
clarity and focus.

Excerpt from Domain Driven Design Quickly

So what the author is saying is that the repository’s (single) responsibility is to encapsulate the logic for obtaining object references. What that means to me is that the class utilizing the repository should be querying it in clear terms, such as:

_repository.GetPendingOrdersFor(customer);

This interface keeps the behavior of the method calling the repository focused on it’s job, and eliminates querying logic. Not only that, but the intention of the code is clear and even human(non-developer) readable.

The alternative is:

_respository.Query<Order>(o => o.CustomerID = customer.CustomerID && o.Status == OrderStatus.Pending);

Here’s how these options would look as an interface.

repository

Just one man’s opinion. What do you think?

Wednesday, July 01, 2009

ASP.Net MVC: Intro To MVCContrib TestHelpers

MVCContrib contains an assembly specifically built to provide help when unit testing ASP.Net MVC controller actions.  This is a very condensed guide to what you can accomplish using each helper method.

Test View Rendering

Test that a view is being rendered from the action:

_controller.List().AssertViewRendered();

…for a particular view

_controller.List().AssertViewRendered().ForView("SomeViewName");

…with a view model of the correct type

_controller.List().AssertViewRendered().ForView("List").WithViewData<SomeType>();

Test PartialView Rendering

Test that a partial view is being rendered from the action:

_controller.ShowAsteroid().AssertPartialViewRendered();

…for a particular view

_controller.ShowAsteroid().AssertPartialViewRendered().ForView("Asteroid");

…with a view model of the correct type

_controller.ShowAsteroid().AssertPartialViewRendered().ForView("Asteroid").WithViewData<AsteroidModel>();

Test Redirects

Assert that the action redirects to the correct action:

_controller.AddFormStar().AssertActionRedirect().ToAction("List");

…for an action on a different controller:

_controller.AddFormStar().AssertActionRedirect().ToController("SomeController").ToAction("List");

Easily Stub HttpContext Variables

The TestHelper library contains a class (TestControllerBuilder) that builds up a complete context for your controller to operate within.  This gives you the ability to easily mock anything that you would normally access from HttpContext.  Here’s an example of usage:

setup

The _builder variable is what contains all of the dictionaries for Session, ViewData, etc. that you can use to set up all of your stubs.  Example:

test

The code in the ActionUnderTest method will now be able to access all of the variables just set up in the test.  Couldn’t be easier!