0

I am new to Google Mock and was trying my hand with this code, I also checked this link.

Actual function call count doesn't match EXPECT_CALL(*mock, display())

but was not able to get proper input.

Base.cc

class Base
{
    int val;
    string msg;
    public:
    Base():val(0), msg("world"){}
    virtual ~Base(){}
    virtual void set(int x, string msg)
    {
            this->val = val;
            this->msg = msg;
    }
    virtual void get()
    {
            cout << "val    :" << this->val << endl;
            cout << "msg    :" << this->msg << endl;
    }
};
class MockBase : public Base
{
    public:
    MOCK_METHOD0(get, void());
    MOCK_METHOD2(set, void(int val, string msg));
};

Base_unittest.cc

int main(int argc, char * argv[])
{
    std::cout << "in main" << endl;
    ::testing::InitGoogleTest(&argc,argv);
    return RUN_ALL_TESTS();
}
TEST(Base, case1)
{
    MockBase obj;
    EXPECT_CALL(obj, get()).WillOnce(::testing::Return());
}
TEST(Base, case2)
{
    MockBase obj;
    EXPECT_CALL(obj, set(2,"hello")).WillOnce(::testing::Return());
}

I am getting error:

Actual function call count doesn't match EXPECT_CALL(obj, get())...

Actual function call count doesn't match EXPECT_CALL(obj, set(2,"hello"))...

So please help and if any tutorials for newbies please refer.

4

1 回答 1

5

您的期望是说,当obj被销毁时,您希望该函数被调用一次。您正在失败,因为该函数实际上并未被调用。

TEST(Base, case1)
{
    MockBase obj;
    EXPECT_CALL(obj, get()).WillOnce(::testing::Return());
    obj.get();
}

将导致此案通过。显然这不是很有用,通常你会将模拟注入到被测对象中。另请注意,这种情况下的操作是不必要的,以下将起作用。

TEST(Base, case1)
{
    MockBase obj;
    EXPECT_CALL(obj, get());
    obj.get();
} 

谷歌有一些很好的参考:

https://github.com/google/googletest/blob/master/googlemock/docs/for_dummies.md

https://github.com/google/googletest/blob/master/googlemock/docs/cheat_sheet.md

https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md

于 2013-05-13T17:32:25.733 回答