How to check an abstract abstract class?

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()
    {
        // arrange
        var filterContext = GetFilterContext(); //method omitted for brevity

        TabsActionFilterTestClass tabsActionFilter =
            new TabsActionFilterTestClass();

        // act
        tabsActionFilter.OnActionExecuted(filterContext);

        // assert
        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"
            };
        }
    }
}
+3
source share
2 answers

, , , , . , , , , GetCustomer -, , .

GetCustomer - , , TabsActionFilter, - GetCustomer , . /.

, , - TypeMock . , , , , .

( TypeMock FWIW).

+2

. , ( factory - ), . - ( ).

public class TabsActionFilter : ActionFilterAttribute 
{ 
    private readonly ICustomerGetter _getter;
    public TabsActionFilter(ICustomerGetter getter)
    { _getter = getter; }

    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
        Customer customer = _getter.GetCustomer(filterContext); 

        ...
    } 
} 
public interface ICustomerGetter
{ 
     Customer GetCustomer(ActionExecutedContext filterContext);
}

TabsActionFilter Mock getter, .

: , , . . , "" ,

public class MyFilter : TabsActionFilter
{
    public MyFilter() : base(new MyGetter()) {}
}

.

+1

Source: https://habr.com/ru/post/1770591/


All Articles