如何设置一个指针属性,以便它可以调用EXPECT_call的函数


class Parent
{
public:
int* x;
};
//is I am trying to make an object of type parent, it results: waiting for specifier after new *
class Child:public Parent
{
void Func()
{
<//DO SOMETHING>
}
};
//Unit test using mock
std::shared_pointer<Child> y =std::make_shared<Mock>();
//set_pointer(y);** //how this can be implemented
EXPECT_CALL(*(std::dynamic_pointer_cast<Mock>(y)).get(),Func()).Times(1);//this test is failed 

如果您的目标是将x指针设置为对Func的调用结果,则可以将Invoke与自定义lambda:一起使用

class Parent {
public:
// most probably this needs a virt. dtor, but if shared_ptrs are to be used, it might be OK
int* x;
};
class Child : public Parent {
public:
virtual void Func() {
//<//DO SOMETHING>
}
};
class ChildMock : public Child {
public:
MOCK_METHOD0(Func, void());
};
TEST(ChildMock, some_test_name) {
// Unit test using mock
std::shared_ptr<Child> y = std::make_shared<ChildMock>();
// set_pointer(y);** //this lambda allows to set the vaule of x -----------------
int some_x = 42;     //                                                         |
EXPECT_CALL(*(std::dynamic_pointer_cast<ChildMock>(y)), Func()).WillOnce(Invoke([&y,&some_x](){
y->x = &some_x;
}));
y->Func();
ASSERT_EQ(42, *y->x);
}

最新更新