HippoMocks嘲笑ref的返回值 [英] HippoMocks mocking return values by ref

查看:154
本文介绍了HippoMocks嘲笑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???;

如何模拟它,以便它将通过参数返回一个std :: vector int(这是方法的返回类型)?

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?

推荐答案

你必须自己提供引用的对象,确保mock使用 with 它传递一个函数到 Do ,这也提供了返回值。它有多少参考参数无关紧要。示例:

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 );

请注意, Do 函数,还有std :: function,lambdas等。

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

这篇关于HippoMocks嘲笑ref的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆