2017-08-25 76 views
0

我有一個受保護的拷貝構造函數的類:谷歌模擬和保護的拷貝構造函數

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) 
    { 
    } 
}; 

和模擬版本:

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); 

如果在模擬測試我打電話'_',它的工作原理:

TEST(Thing, DoSomeThingList) 
{ 
    MockThing thing; 
    ThingList list; 
    MockAnotherThing anotherThing; 

    list.things.push_back(Thing()); 

    EXPECT_CALL(anotherThing, DoListThing(_)); 

    anotherThing.DoListThing(list); 
} 

但是,我怎麼能傳遞在這種情況下列出?如果列表是由DoListThing返回的,那麼我可以使用Return,但是對於像這樣修改的參數,我該如何處理?

回答

0

我無法通過受保護的副本構造函數,所以我的答案是創建一個類的虛擬(虛擬)版本並忽略Google Mock。這對我測試有問題的課程已經足夠好了。我在這裏提供的示例是更大包的簡化版本。