GoogleMock - 匹配器和 MFC\ATL CString
我在 Google 网上论坛上提出了这个问题,但我想在这里我会得到更快的答复。
我正在尝试使用 Google 的 Mocking 框架 来测试我的代码。我也在利用他们的测试框架。我是用VC9编译的。 我在匹配 MFC\ATL CString 参数时遇到问题。 GMock 说对象不相等,并且它似乎正在评估 指针地址。我试图模拟的方法是结构化的 像这样:
void myMethod(const CString & key, const CString & value);
因此:
MOCK_METHOD2(myMethod, void(const CString & key , const CString &
value);
当设置我的期望时,我正在进行以下比较:
CString szKey = _T("Some key");
CString szValue = _T("Some value");
EXPECT_CALL(myMock, myMethod(Eq(szKey), Eq(szValue))).WillOnce(Return
(true));
我尝试了匹配器的许多不同组合,例如:
EXPECT_CALL(myMock, myMethod(StrCaseEq(_T("Some Key")), StrCaseEq(_T
(""Some value)))).WillOnce(Return(true));
EXPECT_CALL(myMock, myMethod(TypedEq<const CString &>(szKey),
TypedEq<const CString &>(szValue))).WillOnce(Return(true));
EXPECT_CALL(myMock, myMethod(TypedEq<const CString &>(szKey),
TypedEq<const CString &>(szValue))).WillOnce(Return(true));
上述任何调用都产生了相同的结果。还有人跑 进入这个问题?
这是输出:
Google Mock 尝试了以下 2 个期望,但没有匹配:
:80: tried expectation #0
Expected arg #1: is equal to 006D430C pointing to "Some value"
Actual: 4-byte object <A8EF 1102>
Expected: to be called once
Actual: never called - unsatisfied and active
:83: tried expectation #1
Expected arg #1: is equal to (ignoring case) ""
Actual: 4-byte object <A8EF 1102>
Expected arg #2: is equal to (ignoring case) "Some value"
Actual: 4-byte object <C0EE 1102>
Expected: to be called once
Actual: never called - unsatisfied and active
Adam
I asked this question on the Google Group but I think I will get a faster response on here.
I'm trying to use Google's Mocking framework to test my code. I am also utilizing their test framework as well. I'm compiling in VC9.
I'm having issues matching arguments that are MFC\ATL CStrings. GMock
says the objects are not equal and it appears it is evaluating on the
pointer addresses. The method I am attempting to mock is structured
like so:
void myMethod(const CString & key, const CString & value);
thus:
MOCK_METHOD2(myMethod, void(const CString & key , const CString &
value);
When setting up my expectations I am doing to following comparison:
CString szKey = _T("Some key");
CString szValue = _T("Some value");
EXPECT_CALL(myMock, myMethod(Eq(szKey), Eq(szValue))).WillOnce(Return
(true));
I have tried many different combinations of the matchers such as:
EXPECT_CALL(myMock, myMethod(StrCaseEq(_T("Some Key")), StrCaseEq(_T
(""Some value)))).WillOnce(Return(true));
EXPECT_CALL(myMock, myMethod(TypedEq<const CString &>(szKey),
TypedEq<const CString &>(szValue))).WillOnce(Return(true));
EXPECT_CALL(myMock, myMethod(TypedEq<const CString &>(szKey),
TypedEq<const CString &>(szValue))).WillOnce(Return(true));
Any of the above calls have produced the same result. Anyone else run
into this issue?
This is the output:
Google Mock tried the following 2 expectations, but none matched:
:80: tried expectation #0
Expected arg #1: is equal to 006D430C pointing to "Some value"
Actual: 4-byte object <A8EF 1102>
Expected: to be called once
Actual: never called - unsatisfied and active
:83: tried expectation #1
Expected arg #1: is equal to (ignoring case) ""
Actual: 4-byte object <A8EF 1102>
Expected arg #2: is equal to (ignoring case) "Some value"
Actual: 4-byte object <C0EE 1102>
Expected: to be called once
Actual: never called - unsatisfied and active
Adam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于当字符串传递给您的方法时您没有复制字符串,因此您真的需要检查它们的值吗?编写以下期望就足够了:
...它将检查提供给模拟方法的字符串确实是您期望的字符串(通过地址验证),而不是副本或其他字符串。
关于为什么预装匹配器不能与
CString
一起使用,我怀疑是因为CString
没有覆盖operator()==
> 或者匹配器实现没有针对CString
的显式专业化。Since you are not making a copy of the strings when they are passed to your method, do you really need to check their values? It should suffice to write the following expectation:
... which will check that the strings given to the mock method are indeed the ones you expect (validated by address), and not a copy or other string.
Regarding why the pre-canned matchers don't work with
CString
, I suspect it is either becauseCString
doesn't overrideoperator()==
or the matcher implementations don't have an explicit specialization forCString
.最终出现了不同的错误。 叹息它实际上是在捕获一个错误......Google Mocks 可以很好地比较 CStrings。
Ended up being a different error. sighs It was actually catching a bug.... Google Mocks can compare CStrings just fine.