实际函数调用次数与EXPECT_CALL(mockImplClass, receive(_, _))不匹配。

问题描述 投票:0回答:1

我在运行gtest时遇到了问题,下面的代码sample.ignore header includes是可以编译的,而且运行正常。

Error:
GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: receive(0x7ffcee4fc990, 0x7ffcee4fc900)
          Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect for details.
/data/home/sipadhy/unit_test_research/gTest/ImplClassTest.cpp:174: Failure
Actual function call count doesn't match EXPECT_CALL(mockImplClass, receive(_, _))...
         Expected: to be called at least once
           Actual: never called - unsatisfied and active

示例代码。

/ 主类中需要模拟的函数。

class ImplClass
{
public:
    virtual int receive(structX* x, structY* y){ // some logic }
};

/ 一个中间类,它调用主类。

class IntermidiateClass
{
    std::shared_ptr<ImplClass> implClassPtr = nullptr;
public:
    setImplClassptr(std::shared_ptr<ImplClass> ptr)
    {
        implClassPtr  = ptr;
    }
    int getValue()
    {
        structX x;
        structY y;
        return(implClassPtr->receive(x, y));
    }
};

/ 模拟班

class MockImplClass: public ImplClass
{
public:
    MOCK_METHOD2(receive, int(structX, structY));
}

/测试案例

TEST(MyTest, TEST1)
{
    MockImplClass mockImplClass;
    IntermidiateClass intermidiateObj;
    intermidiateObj.setImplClassptr(std::make_shared<MockImplClass>());

    EXPECT_CALL(mockImplClass, receive(_, _))
    .Times(AtLeast(1))
    .WillRepeatedly(Return(1));

    int retVal = intermidiateObj.getValue();
}

谢谢,Siva

c++11 googletest gmock
1个回答
0
投票

你创建了一个全新的对象 MockImplClass 类。

std::make_shared<MockImplClass>()

因此你的第一个创建对象

MockImplClass mockImplClass;

不习惯叫 receive()

© www.soinside.com 2019 - 2024. All rights reserved.