jeudi 4 janvier 2018

Test 3rd Party Assembly Wrapper

I have a 3rd party assembly that I've a simple interface for along the lines of:

interface IFoo
{
    double Bar(List<double> aList);
}

For simplicity a list of numbers is to be sent to the implementation, and a result returned... here an average of a list (mean, median, mode) .. i have done this for the sole reason of avoiding direct references if we want to change the 3rd party, or even write our own.. i deliberately want to be able to select the way the average is done.

Two questions:

1) Multiple average methods can be called and yield a result, so concrete classes i have implemented inherit from IFoo along the lines of...

public class FooMeanAvg : IFoo
{
    public double Bar(List<double> aList)
    {
        // find mean
    }
}

public class FooMedianAvg : IFoo
{
    public double Bar(string s1)
    {
        // find median
    }
}

To then utilise this, i was contemplating a factory but it seemed overkill when all that really needed to be done each time was:

IFoo f = new FooMedianAvg();
var result = f.Bar(List);

Now i am thinking what have i really achieved??... all i need to do is be able to select different implementations of the interface when necessary.. is there a better way to do this?

Should i be looking at another pattern? Sorry, very new to patterns.. facade, strategy etc?

2) In terms of writing a unit test for the functionality.. the interface is not public, and although it could be made public (not sure if correct).. so i can't really just do:

IFoo f = new FooMeanAvg();
var result = f.Bar(List);
Assert.AreEqual(expected, result);

in the test project.. i get a bit lost at this point really.. mocking, dependency injection, and it gets a bit hazy..

Ty!

Aucun commentaire:

Enregistrer un commentaire