我请求您使用指向模拟对象的指针设置期望的输入/建议/帮助。 堆栈在CPP中,应用程序使用new运算符创建堆栈类的对象。 在单元测试中,需要在源代码中创建的模拟对象上设置期望值。 非常感谢。
请在下面找到代码段:
StackBT.h
class StackBT
{
MOCK_METHOD0(registerCallbacks, void());
MOCK_METHOD2(...)
MOCK_METHOD4(...)
}
ApplicationBT.h:
class ApplicationBT
{
public:
ApplicationBT() : mpoStackBT(new StackBT())
void init()
{
mpoStackBT->registerCallbacks();
}
friend class TestApplicationBT;
StackBT* mpoStackBT;
}
TestApplicationBT.h
class TestApplicationBT : public ::testing::Test
{
protected:
virtual void SetUp ()
{
ptrApplicationBT = new ApplicationBT();
}
void TearDown()
{
delete ptrApplicationBT;
}
public:
TestApplicationBT ()
{
}
~TestApplicationBT ()
{
ptrApplicationBT = NULL;
}
scoped_ptr<ApplicationBT> ptrApplicationBT;
};
TEST_F(TestApplicationBT, TEST_FUNC_CALL_IN_MOCK_CREATED_INSIDE_ApplicationBT_USING_NEW)
{
EXPECT_CALL(ptrApplicationBT->mpoStackBT, registerCallbacks()).Times(1);//Need to pass the object instead of the reference to StackBT
ptrApplicationBT->init();
}