【问题标题】:Google Mock and protected copy constructorGoogle Mock 和受保护的复制构造函数
【发布时间】:2018-02-03 15:26:29
【问题描述】:

我有一个带有受保护的复制构造函数的类:

class ThingList
{
public:
    ThingList() {}
    virtual ~ThingList() {}

    std::vector<Thing> things;

protected:
    ThingList(const ThingList &copy) {}
};

我有另一个班级使用这个:

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


    【解决方案1】:

    我无法通过受保护的复制构造函数,所以我的答案是创建一个类的假(虚拟)版本并忽略 Google Mock。这足以让我测试有问题的课程。我在这里提供的示例是大包的简化版本。

    【讨论】:

      猜你喜欢
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 2015-08-12
      相关资源
      最近更新 更多