Search code examples
c++unit-testingmockinghippomocks

HippoMocks mocking return values by ref


class IEmployeeServiceProxy
{
public:
    virtual ~IEmployeeServiceProxy() { }
    virtual void AddEmployee(const Employee&) = 0;
    virtual int GetEmployees(std::vector<Employee>&) = 0;
};

struct Employee
{
    boost::uuids::uuid Id;
    std::string Name;
};

m_Mocks.ExpectCall(m_EmpSvcMock.get(), IEmployeeServiceProxy::GetEmployees).Return???;

How do I mock it so that it'll return a std::vector via the argument instead of int (which is the return type of the method)?

Also, what if there is more than 1 ref argument?


Solution

  • you have to provide the object for the reference yourself, make sure the mock uses it using With and you can alter it passing a function to Do, which also provides the return value. It does not matter how many reference arguments there are. Example:

    int AddSomeEmployees( std::vector< Employee >& v )
    {
      v.push_back( Employee() );
      return 0;
    }
    
      //test code
    std::vector< int > arg;
    
    mocks.ExpectCall( empSvcMock, IEmployeeServiceProxy::GetEmployees ).With( arg ).Do( AddSomeEmployees );
    

    Note that Do can take any kind of function, also std::function, lambdas etc.