vendredi 10 février 2017

Convention for stubbing out library code for test purposes

My application invokes some library code while doing some stuff. I would like to stub out this library code for test purposes.

To achieve this I have created an interface between the application code and the library. It's a bit like an adapter, only its not modifying the interface of the library.

This is a simplified version of what I have come up with.

class LibInterface
{
    virtual void foo() = 0;
};

class LibInterfaceImpl
{
    virtual void foo()
    {
        // make calls into the library code 
    }
};

The application calls foo through a LibInterface&

m_libInterface.foo();

For a production build, I instantiate LibInterfaceImpl, which makes real calls into the library and supply a reference to it to the application. But I can now test the application without pulling in the library by supplying it with a stub for LibInterface that does whatever I want.

Is this approach OK? Or is there a better way? Is there a convention for naming such an interface class. The nearest I've found is an adapter, but as stated above it's not altering the library interface. More like providing a stubbable wrapper around it.

Aucun commentaire:

Enregistrer un commentaire