2016-11-25 44 views
1

我有嘲笑接口匹配的std :: wstring的在googlemocks EXPECT_CALL

// Interface 
class MyInterface 
{ 
    void get(const std::wstring& param) = 0; 
} 

// Mock interface 
class MyInterfaceMock : public MyInterface 
{ 
    MOCK_METHOD1(get, void(const std::wstring& param)); 
} 

Exemplaric測試方法:

... 
EXPECT_CALL(myInterfaceMock, L"hello"); 

當我編譯它(vs2015)我得到的消息

錯誤C2664:'testing :: internal :: MockSpec ...:不能將參數1從'const wchar_t [6]'轉換爲'const testing :: Matcher &'

跟消息: 原因:不能從 '常量爲wchar_t [7]' 到 'const的測試::匹配器'

轉換當我使用的std :: string代替的std :: wstring的的一切工作正常。有誰知道爲什麼std :: wstring不能匹配?

+0

什麼版本的google-mock?目前的? – PiotrNycz

+0

我正在使用gmock-1.6.0 – anhoppe

回答

1

我猜你的意思EXPECT_CALL(myInterfaceMock, get(L"hello"));

你應該寫EXPECT_CALL(myInterfaceMock, get(std::wstring(L"hello")));一切都應該工作。

真正的問題是爲什麼匹配器std::string接受const char*作爲價值。答案是 - 因爲谷歌 - 模擬庫支持這個意向 - 見code

template <> 
class GTEST_API_ Matcher<internal::string> 
    : public internal::MatcherBase<internal::string> { 
public: 
    Matcher() {} 

    explicit Matcher(const MatcherInterface<internal::string>* impl) 
     : internal::MatcherBase<internal::string>(impl) {} 

    // Allows the user to write str instead of Eq(str) sometimes, where 
    // str is a string object. 
    Matcher(const internal::string& s); // NOLINT 

    // Allows the user to write "foo" instead of Eq("foo") sometimes. 
    Matcher(const char* s); // NOLINT 
}; 

還有就是Matcher<T>std::wstring沒有相應的專業化。我建議你不要添加一個 - 因爲它將來可能會改變 - 這是gmock的實現細節。相反,您可能會要求gmock開發者以與string相似的方式添加對wstring的支持...順便提一下,我已經加入one

+0

很好的答案,非常感謝。這解釋了一切。順便說一下,我的意思是EXPECT_CALL(myInterfaceMock,get(L「hello」)); – anhoppe