2017-01-15 84 views
0

我在玩gmock,我有一個人爲的例子,我用它來學習它的細微差別。我有什麼,我本來期望的隱含拷貝構造函數的調用問題:gmock調用隱式刪除的拷貝構造函數

// mock_word.h 
class MockWord : Word { 
public: 
    MockWord(const std::string word) : Word(word) {}; 
    MOCK_METHOD0(pigLatinify, std::string(void)); 
}; 

// strings.h 
template <typename Word> 
class Strings { 
... 
private: 
    std::vector<Word>* words = new std::vector<Word>(); 
public: 
    // This should call the implicit copy constructor 
    void addWord(const Word word) { 
     this->words->push_back(word); 
    }; 
... 
}; 

// strings_test.cpp 
class StringsTest : public ::testing::Test { 
protected: 
    Strings<MockWord>* strings; 
public: 
    virtual void SetUp() { 
     strings = new Strings<MockWord>(); 
    }; 
    virtual void TearDown() { 
     delete strings; 
    }; 
}; 

TEST_F(StringsTest, StringIsAllPigLatinifiedNicely) { 
    MockWord mockWordA("beast"); 
    MockWord mockWordB("dough"); 

    // Set some expectations for the Mock 
    EXPECT_CALL(mockWordA, pigLatinify()).Times(AtLeast(1)); 
    EXPECT_CALL(mockWordB, pigLatinify()).Times(AtLeast(1)); 

    strings->addWord(mockWordA); 
    strings->addWord(mockWordB); 
    ... 
}; 

現在,我大概可以有這樣的請編譯,如果我轉換mockWordAmockWordB從自動變量的指針工作,但是這不是我想提供的界面。

我得到確切的錯誤是:

error: call to implicitly-deleted copy constructor of 'MockWord' 
    strings->addWord(mockWordA); 
        ^~~~~~~~~ 
mock_word.h:11:9: note: copy constructor of 'MockWord' is implicitly  deleted because field 'gmock0_pigLatinify_11' has a deleted copy  constructor 
    MOCK_METHOD0(pigLatinify, std::string(void)); 
+0

mock from google mock are not copyable – PiotrNycz

+0

[可以在設置期望值後複製google模擬對象嗎?](http://stackoverflow.com/questions/33043640/can-i-copy-a-google-模擬對象後設置的期望) – PiotrNycz

+0

所以我只能通過使我的對象是一個指針類型來實現這個基本的測試?這似乎是限制性的。 – wulfgarpro

回答

0

我解決了通過改變我的功能界面的問題Strings::addWord(Word* word)這對我因爲我寧可不使用指針與這樣一個基本的例子似乎限制。

相關問題