【发布时间】:2018-02-03 15:26:29
【问题描述】:
我有一个带有受保护的复制构造函数的类:
class ThingList
{
public:
ThingList() {}
virtual ~ThingList() {}
std::vector<Thing> things;
protected:
ThingList(const ThingList ©) {}
};
我有另一个班级使用这个:
class AnotherThing
{
public:
AnotherThing()
{
}
virtual ~AnotherThing() {}
void DoListThing(const ThingList &list)
{
}
};
还有这个类的 Mock 版本:
class MockAnotherThing : public AnotherThing
{
public:
MOCK_METHOD1(DoListThing, void(const ThingList &list));
};
我想用一个真实的参数调用这个方法 DoListThing 来提供一个真实的列表:
TEST(Thing, DoSomeThingList)
{
MockThing thing;
ThingList list;
MockAnotherThing anotherThing;
list.things.push_back(Thing());
EXPECT_CALL(anotherThing, DoListThing(list));
anotherThing.DoListThing(list);
}
编译时出错:
1>..\mockit\googletest\googlemock\include\gmock\gmock-matchers.h(3746): error C2248: 'ThingList::ThingList': cannot access protected member declared in class 'ThingList'
但是,如果我进行非模拟调用,它就可以正常工作:
ThingList list;
AnotherThing theRealThing;
theRealThing.DoListThing(list);
如果在 Mock 测试中我用 '_' 调用,它会起作用:
TEST(Thing, DoSomeThingList)
{
MockThing thing;
ThingList list;
MockAnotherThing anotherThing;
list.things.push_back(Thing());
EXPECT_CALL(anotherThing, DoListThing(_));
anotherThing.DoListThing(list);
}
但是,在这种情况下如何传递列表?如果列表由 DoListThing 返回,我可以使用 Return,但是对于这样修改的参数,我该怎么办?
【问题讨论】:
标签: constructor copy protected googlemock