1
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?

BЈовић
  • 62,405
  • 41
  • 173
  • 273
Zach Saw
  • 4,308
  • 3
  • 33
  • 49

2 Answers2

2

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.

stijn
  • 34,664
  • 13
  • 111
  • 163
  • Ah that's helpful! Thank you. – Zach Saw Aug 02 '12 at 07:53
  • Could you add an example of doing it via C++0x/11 lambdas? I tried `Do([] -> int (std::vector& employees){})` but GCC 4.5.3 kept complaining no matching function to TCall<...>.Do(...). – Zach Saw Aug 03 '12 at 02:31
  • Found the problem. It's HippoMocks declaring Do's argument as ref, and a ref to temp lambda isn't allowed. Submitted patch to Dascandy. – Zach Saw Aug 03 '12 at 03:31
2

The Git version (most recent one) has an option for Out parameters which are pretty much this. To use

std::vector<int> args; args.push_back(1); args.push_back(2);
mocks.ExpectCall(mock, IInterface::function).With(Out(arg));
dascandy
  • 7,184
  • 1
  • 29
  • 50
  • Is the Git version similar to SVN version? If not, where can I get the Git version (tarballed preferably)? – Zach Saw Aug 02 '12 at 07:56
  • http://www.assembla.com/code/hippomocks/git/nodes is the git version that I last tested myself (there's a newer commit branch on Github but that contains some changes I haven't checked yet). It should be similar but newer. As far as I remember the only thing that actually changed (As opposed to was added) was that the class mock no longer initializes members but there's a new function MemberMock that replaces it. You probably didn't use this before as it's not a typical thing to use. – dascandy Aug 02 '12 at 08:01
  • @dascandy you know if hippomocks is still actively developped? – stijn Aug 02 '12 at 08:05
  • @stjin: Dascandy is the developer :) – Zach Saw Aug 02 '12 at 08:07
  • @dascandy Could you maybe give some more documentation/explanation on how to use the "In" and "Out" options? – Dan Feb 09 '22 at 17:21