HippoMock always throwing NotImplemented

611 views Asked by At

I'm attempting to mock an Interface using HippoMock, for use in a class that makes use of said interface. I build a mock object and setup the ExceptCallOverload, everything compiles fine. However the the class I am testing makes a call to the mock object it calls the mock::NotImplemented function.

Also, the routines I am mocking accept references to interfaces, but the objects that are being passed in a are stored in a shared_ptr. If I wall .With and pass the shared_ptr object I get an error reporting comparer::compare can't match template parameters, this is understandable. So if I just pass in the reference to the interface I get an error can't a pure-virtual class can't be instantiated.

I feel like this puts me between a rock and a hard place with regards to using HippoMark.

a small example:

class objectA_interface
{
public:
   virtual double getDouble() = 0;
};

class objectB_interface
{
public:
   virtual double getDouble() = 0;
};

class test_interface
{
public:
   virtual void fun(objectA_interface&) = 0;
   virtual void fun(objectB_interface&) = 0;
};

void do_something()
{
   std::shared_ptr<objectA_interface> objectA;
   std::shared_ptr<objectB_interface> objectB;

   MockRepository mocks;
   test_interface* mock_interface = mocks.Mock<test_interface>();

   //error C2259: 'object_interface' : cannot instantiate abstract class
   mocks.ExpectCallOverload(mock_interface, (void (test_interface::*)(objectA_interface&))&test_interface::fun).With(*objectA);
   mocks.ExpectCallOverload(mock_interface, (void (test_interface::*)(objectB_interface&))&test_interface::fun).With(*objectB);

   //error C2665: 'HippoMocks::comparer<A>::compare' : none of the 2 overloads could convert all the argument types
   mocks.ExpectCallOverload(mock_interface, (void (test_interface::*)(objectA_interface&))&test_interface::fun).With(objectA);
   mocks.ExpectCallOverload(mock_interface, (void (test_interface::*)(objectB_interface&))&test_interface::fun).With(objectB);
}
1

There are 1 answers

0
dascandy On BEST ANSWER

You can't pass in a shared_ptr as a flat reference, to any function really. That explains why the second call doesn't work - a shared_ptr is not a reference.

The first call should work, but it looks like .With is trying to make a copy of your object rather than using the reference. You can use Hippomocks::byRef to explicitly indicate it should use this as a reference rather than a copyable instance so that it will use your instance instead. Doing this implicitly results in a likely crash for temporaries that go out of scope before the actual call happens.

From the test code in test_ref_args.cpp, your exact case:

class IRefArg {
public:
        virtual void test() = 0;
};

    MockRepository mocks;
    IK *iamock = mocks.Mock<IK>();
    IRefArg *refArg = mocks.Mock<IRefArg>();

    mocks.ExpectCall(iamock, IK::l).With(byRef(*refArg));
    iamock->l(*refArg);