I worked on a better way to test an abstract class with a name TabsActionFilter. I guaranteed that classes inheriting from TabsActionFilterwould have a method called GetCustomer. In practice, this design works well.
Where I am having problems, you will learn how to test the OnActionExecutedbase class method . This method is based on the implementation of a secure abstract method GetCustomer. I tried to taunt the class using Rhino Mocks , but doesn't seem to mock the fake client coming back from GetCustomer. Obviously, using this method for the public will make it mocking, but protected feels like a more appropriate level of accessibility .
Currently, in my test class, I have added a specific private class that inherits from TabsActionFilterand returns a fake Customer object.
- Is a particular class the only option?
- Is there a simple bullying mechanism that I am missing that will allow Rhino Mocks to provide a refund for
GetCustomer?
As a note, Anderson Imes discusses his opinion on this in the answer about Moq , and I might be missing some key, but it is not applicable here.
The class to be tested
public abstract class TabsActionFilter : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Customer customer = GetCustomer(filterContext);
List<TabItem> tabItems = new List<TabItem>();
tabItems.Add(CreateTab(customer, "Summary", "Details", "Customer",
filterContext));
tabItems.Add(CreateTab(customer, "Computers", "Index", "Machine",
filterContext));
tabItems.Add(CreateTab(customer, "Accounts", "AccountList",
"Customer", filterContext));
tabItems.Add(CreateTab(customer, "Actions Required", "Details",
"Customer", filterContext));
filterContext.Controller.ViewData.PageTitleSet(customer.CustMailName);
filterContext.Controller.ViewData.TabItemListSet(tabItems);
}
protected abstract Customer GetCustomer(ActionExecutedContext filterContext);
}
Testing class and private class for "ridicule"
public class TabsActionFilterTest
{
[TestMethod]
public void CanCreateTabs()
{
var filterContext = GetFilterContext();
TabsActionFilterTestClass tabsActionFilter =
new TabsActionFilterTestClass();
tabsActionFilter.OnActionExecuted(filterContext);
Assert.IsTrue(filterContext.Controller.ViewData
.TabItemListGet().Count > 0);
}
private class TabsActionFilterTestClass : TabsActionFilter
{
protected override Customer GetCustomer(
ActionExecutedContext filterContext)
{
return new Customer
{
Id = "4242",
CustMailName = "Hal"
};
}
}
}