mercredi 2 septembre 2015

Abstract Factory Unit Testing In Moq

Suppose I have following class called SomeClass as defined:

public SomeClass
{
    private IThingFactory _thingFactory;

    public SomeClass(IThingFactory thingFactory)
    {
        _thingFactory = thingFactory;
    }

    public IThing CreateThing(int a, int b, string c)
    {
        IThing thing = _thingFactory.MakeEmptyThing();
        thing.MakeFromFields(a, b, c);

        return thing;
    }
}

The spec of the CreateThing(int a, int b, string c) method is that it takes an int a, int b, string c and returns an IThing with the corresponding properties. (so internally maybe new Thing(a, b, c))

However the implementation delegates that work to a IThingFactory and Thing populates itself.

So now I'm trying to unit test the method CreateThing(), but I'm not exactly sure how it should work.

Here's what I tried:

Based off of the principles of mocking dependencies, I created a Mock<IThingFactory> and gave it to the constructor of SomeClass

_mockThingFactory = new Mock<IThingFactory>();
someClass = new SomeClass(_mockThingFactory.Object);

Afterwards, I called the method under test

IThing thing = _someClass.CreateThing(It.IsAny<int>(), 
    It.IsAny<int>(), 
    It.IsAny<string>>());

And to assert, I'm not sure. Should I verify that MakeEmptyThing() was called? What do I do with thing.MakeFromFields(a, b, c);?

Aucun commentaire:

Enregistrer un commentaire