How do I make this ASP.NET MVC controller more testable?

Posted by Ragesh on Stack Overflow See other posts from Stack Overflow or by Ragesh
Published on 2010-06-09T17:52:10Z Indexed on 2010/06/09 20:42 UTC
Read the original article Hit count: 257

I have a controller that overrides OnActionExecuting and does something like this:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting(filterContext);
    string tenantDomain = filterContext.RouteData.Values["tenantDomain"] as string;
    if (!string.IsNullOrWhiteSpace(tenantDomain))
    {
        using (var tx = BeginTransaction())
        {
            this.Tenant = repo.FindOne(t => t.Domain == tenantDomain);
        }
    }
}

Tenant is a protected property with a private setter. The class itself is an abstract base controller that my real controllers derive from. I have code in other controllers that looks a lot like this:

if (Tenant == null)
{
   // Do something
}
else
{
   // Do something else
}

How do I test this code? What I need to do is to somehow set the Tenant property, but I can't because:

  1. It's a protected property, and
  2. It has a private setter

Changing the visibility of Tenant doesn't "feel" right. What are my alternatives to unit test my derived controllers?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about unit-testing