Using wxString with Google Mock
Using wxString with Google Mock
有没有人在与wxWidgets一起使用Google Mock时运气不错?我有一个类Foo,其setter在签名中接受对wxString的const引用,如下所示:
class Foo {
public:
Foo();
virtual ~Foo();
void setName(const wxString& name);
};
然后我继续像这样模拟Foo:
class MockFoo : public Foo {
MOCK_METHOD1(setName, void(const wxString& name));
};
我的其他模拟工作得很好,但有一些关于wxString参数,它不喜欢。当我编译时,我看到以下内容:
C:gmock-1.6.0gtestincludegtestinternalgtest-internal.h:890: error: conversion from `const wxUniChar' to `long long int' is ambiguous
C:wxWidgets-2.9.0includewxunichar.h:74: note: candidates are: wxUniChar::operator char() const
C:wxWidgets-2.9.0includewxunichar.h:75: note: wxUniChar::operator unsigned char() const
//more potential candidates from wxUniChar follow after that
问题在于Google Mock不能确定调用哪个operator()函数,因为wxUniChar提供的operator()函数不映射到Google Mock所期望的。我看到"long long int"answers"testing::internal::BiggestInt"转换出现此错误。
这一定是使用代理类wxUniCharRef
作为wxString::operator[]()
的结果类型的结果(请参阅wxString文档的"粗心的陷阱"部分了解更多细节),但我不确定它究竟来自何处,因为这里似乎没有任何访问wxString字符的代码。gtest-internal.h
的第890行到底是什么?
同样,你说你正在使用对wxString的const引用,但你的代码没有。我认为这与你的问题无关但是描述和代码片段之间的差异让人很困惑。
在wxUniChar头文件中添加以下内容似乎可以工作:
wxUniChar(long long int c) { m_value = c; }
operator long long int() const { return (long long int)m_value; }
wxUniChar& operator=(long long int c) { m_value = c; return *this; }
bool operator op(long long int c) const { return m_value op (value_type)c; }
wxUniCharRef& operator=(long long int c) { return *this = wxUniChar(c); }
operator long long int() const { return UniChar(); }
bool operator op(long long int c) const { return UniChar() op c; }
我将这些插入到头文件的适当部分中,编译错误就消失了。如果这听起来像是一个合理的解决方案,那么如果我有时间的话,我会用一些单元测试来为wxWidgets做一个补丁。
相关文章:
- 如何使用Google Mock来模拟gettimeofday()
- gmock / google-mock发出警告,并且通过模拟异常未通过测试
- 如何期望通过使用Google Test(Mock)以特定频率调用函数
- Google Mock:在目标类的构造函数中实例化的模拟私有变量成员
- Google Mock Expect_call将退休另一个期望
- Google-Mock 一个已经声明的方法
- Google Mock - 使用 CoTaskMemFree 时,测试返回没有结果
- 如何在内联回调函数中使用EXPECT_EQ和其他Google Mock语句
- C Google Mock -Expect_Call() - 期望在不直接调用时无法正常工作
- C++ 使用Google Mock进行高性能单元测试
- 我用纯虚拟方法和Google Mock做错了什么
- 使用 Google C++ 模拟框架 (Google Mock) (V1.5) 将任意参数传递给调用的方法
- 使用 google mock 模拟转换运算符 int()
- 如何使用Google Mock避免指针
- 如何将Given/Whin/Then模式与Mock一起使用(Google Mock)
- 如何使用Google Mock更改指定对象的类型
- 避免匹配.在Google Mock中多次WillOnce
- Compiling Google Mock for iOS
- 使用Google Mock实现Mock方法
- 使用Google Mock来模拟在客户端代码中复制的对象