Factory Method Unit Testing

Suppose I have several OrderProcessors, each of which handles the order a little differently. The decision about which one to OrderProcessoruse is made in accordance with the properties of the object Orderand is performed using the factory method, for example:

public IOrderProcessor CreateOrderProcessor(IOrdersRepository repository, Order order, DiscountPercentages discountPercentages)
{
    if (order.Amount > 5 && order.Unit.Price < 8)
    {
        return new DiscountOrderProcessor(repository, order, discountPercentages.FullDiscountPercentage);
    }

    if (order.Amount < 5)
    {
        // Offer a more modest discount
        return new DiscountOrderProcessor(repository, order, discountPercentages.ModestDiscountPercentage);
    }

    return new OutrageousPriceOrderProcessor(repository, order);
}

Now my problem is that I want to check that the returned one OrderProcessorreceived the correct parameters (for example, the correct discount percentage).
However, these properties are not published to objects OrderProcessor.

How would you suggest me deal with this scenario?

The only solution I could come up with was to make the percentage discount property for OrderProcessorpublicly available, but it seems like it's too hard to do for unit testing ...

+5
3

, , , , , . : http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx

- AssemblyInfo.cs:

[assembly:InternalsVisibleTo("Orders.Tests")]

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

unit test (DiscountOrderProcessor ..) /. factory, .

, , . , ​​ .

+3

, IOrderProcessor . DiscountOrderProcessor, , .

+1

, . DiscountOrderProcessor:

public class FullDiscountOrderProcessor : DiscountOrderProcessor
{
    public FullDiscountOrderProcessor(IOrdersRepository repository, Order order):base(repository,order,discountPercentages.FullDiscountPercentage)
    {}
}

public class ModestDiscountOrderProcessor : DiscountOrderProcessor
{
    public ModestDiscountOrderProcessor (IOrdersRepository repository, Order order):base(repository,order,discountPercentages.ModestDiscountPercentage)
    {}
}

.

factory DiscountOrderProcessor, , , .

DiscountOrderProcessor , .

, , , - , . , FullDiscountOrderProcessor.

You need to somehow check the actual values ​​that will leave you:

you can make properties public (or internal — using InternalsVisibleTo) so you can interrogate them.

you can take the returned object and check if it applies the discount correctly to some object that you pass to it.

Personally, I would like to make the properties internal, but it depends on how the objects interact and if the transfer of the mock object to the discount order processor and checking the correctness of its action is simple, this may be a better solution.

0
source

All Articles