I have the following test:
TestGet(): _interface(), _db(_interface)
{
_interface.get = mockGet;
}
which is used when testing this class:
class DB: public IDB
{
public:
explicit DB(Interface_T& interface):
_interface(interface)
{
}
...
private:
Interface_T _interface;
};
Interface_T is a C interface implemented in a struct and passed to me from a C api. I wish to use the DB class as a wrapper around the C interface.
Notice however that DB copies the interface object to its member _interface. Therefore the line:
_interface.get = mockGet;
has no effect from the DB objects point of view although this was the intention when I wrote the test class. How would you rewrite TestGet() to remedy this error? How would you present to the client of the DB class that it copies the value passed to it?
Presuming that your intention is for
TestGetto set a member on theInterface_Tobject used byDB, you can:A. Defer construction of
DB:B. If you have control over the
Interface_Tclass, you could add a constructor that initializesInterface_T::getdirectly. Then you could do:C. If you have control over the
DBclass, you could change it to share ownership of the suppliedInterface_T(e.g. throughboost::shared_ptr), add a constructor as in B, or add an accessor to its internalInterface_Tmember.