β

How to match argument reference in Google Mock

User zangw - Stack Overflow 226 阅读
c++

I am newer for google mock. Now I have one question about how to match the argument reference? here are the codes

class Parameter {
public:
    int m_idata;
    char m_cdata;
    bool Parameter::operator ==(const Parameter& element) const {
        return (m_idata == element.m_idata && m_cdata == element.m_cdata);
    }
};
class FooInterface {
public:
    virtual ~FooInterface() {}
    virtual void SetParameter(Parameter& val) = 0;
};
// mock class
class MockFoo: public FooInterface {
public:        
    MOCK_METHOD1(SetParameter, void(Parameter& val));
};
TEST(FooTest, setParameterTest) {   
    MockFoo mockFoo;
    EXPECT_CALL(mockFoo, SetParameter(An<Parameter&>())); // How to match argument reference???
    Parameter para;
    mockFoo.SetParameter(para);  // there is an exception here, why???
}

and I also tried the following to match SetParameter():

Parameter test_para;
EXPECT_CALL(mockFoo, SetParameter(Ref(test_para)));

And

EXPECT_CALL(mockFoo, SetParameter(A<Parameter&>()));

And

Parameter test_para;
test_para.m_cdata = 'c';
test_para.m_idata = 10;
EXPECT_CALL(mockFoo, SetParameter(_)).WillOnce(::testing::SetArgReferee<0>(test_para));

Both those two codes can also cause exception... Could any one tell me how to match the argument reference Parameter& in the function SetParameter()?

c++
作者:User zangw - Stack Overflow
most recent 30 from stackoverflow.com
原文地址:How to match argument reference in Google Mock, 感谢原作者分享。

发表评论