HippoMocks:如何预期呼叫通过引用传递的参数?

时间:2017-11-02 19:27:52

标签: c++ testing hippomocks

在使用Hippomocks时,我们很难期望将一个byref参数传递给函数。

class A
{
public:
    virtual ~A();
    virtual void foo(IBar& callback, const unsigned x);
};

IBar是一个界面。

然后我有一个调用的函数:

void Service::baz()
{a.foo(callback, 1);}

以下是测试:

TEST_FIXTURE(PigScalesServiceTest, test_something)
{
    A* aMock = mockRepository.Mock<A>();
    IBar* callback = mockRepository.Mock<IBar>(); 
    Service service(*amock, *callback);

    mockRepository.ExpectCall(aMock, A::foo).With(*callback, 1);
    service.baz();
}

运行测试会出现以下错误:

Function A::foo(???,1) called with mismatching expectation!
Expections set:
Expectation for A::foo(???,1) on the mock at 0x0x10969908 was not satisfied.

非常感谢!

1 个答案:

答案 0 :(得分:0)

我认为你应该使用这样的东西:

mockRepository.ExpectCall(aMock,A :: foo).With(byRef(* callback),1);

看看HippoMocks本身的单元测试: https://github.com/dascandy/hippomocks/blob/master/HippoMocksTest/test_ref_args.cpp