【问题标题】:Matching std::wstring in googlemocks EXPECT_CALL在 googlemocks EXPECT_CALL 中匹配 std::wstring
【发布时间】:2016-11-25 14:22:28
【问题描述】:

我有模拟界面

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

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

示例测试方法:

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

当我编译它时(vs2015)我收到消息

错误 C2664: 'testing::internal::MockSpec...: 无法将参数 1 从 'const wchar_t [6]' 转换为 'const testing::Matcher &'

接着是消息: 原因:无法从 'const wchar_t [7]' 转换为 'const testing::Matcher'

当我使用 std::string 代替 std::wstring 时,一切正常。有人知道为什么 std::wstring 不能匹配吗?

【问题讨论】:

  • 什么版本的 google-mock?当前的?
  • 我正在使用 gmock-1.6.0

标签: c++ stl googlemock wstring


【解决方案1】:

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

你应该写EXPECT_CALL(myInterfaceMock, get(std::wstring(L"hello")));一切都应该工作。

真正的问题是为什么来自std::string 的匹配器接受const char* 作为值。答案是 - 因为 google-mock 库有意支持这一点 - 请参阅 code:

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

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

  template <typename M, typename = typename std::remove_reference<
                            M>::type::is_gtest_matcher>
  Matcher(M&& m)  // NOLINT
      : internal::MatcherBase<std::string>(std::forward<M>(m)) {}

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

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

Matcher&lt;T&gt; 没有与 std::wstring 等效的特化。我建议你不要添加一个 - 因为它将来可能会改变 - 这是 gmock 实现细节。相反,您可能会要求 gmock 开发人员以与 string 类似的方式添加对 wstring 的支持...顺便说一句,我已经添加了 one

【讨论】:

  • 很好的答案,非常感谢。这就解释了一切。而且,顺便说一句,我的意思当然是 EXPECT_CALL(myInterfaceMock, get(L"hello"));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多