How do I make this ASP.NET MVC controller more testable?
- by Ragesh
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:
It's a protected property, and
It has a private setter
Changing the visibility of Tenant doesn't "feel" right. What are my alternatives to unit test my derived controllers?