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



在为以下代码示例运行gtest时遇到问题。忽略头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

您在此处创建MockImplClass类的全新对象:

std::make_shared<MockImplClass>()

因此,您的第一个创建对象

MockImplClass mockImplClass;

从不习惯调用receive()

最新更新