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 (这是该方法的返回类型)?
另外,如果有超过1个ref参数怎么办?
发布于 2012-08-02 15:49:23
你必须自己提供引用的对象,确保模拟使用With来使用它,并且你可以通过向Do传递一个函数来修改它,它也提供返回值。有多少引用参数并不重要。示例:
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等。
发布于 2012-08-02 15:55:09
Git版本(最新的版本)有一个Out参数的选项,几乎是这样的。要使用
std::vector<int> args; args.push_back(1); args.push_back(2);
mocks.ExpectCall(mock, IInterface::function).With(Out(arg));https://stackoverflow.com/questions/11772970
复制相似问题