Question

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

Was it helpful?

Solution

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:

CString szKey = _T("Some key");
CString szValue = _T("Some value");

EXPECT_CALL(myMock, myMethod(szKey, szValue)).WillOnce(Return(true));

... 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 because CString doesn't override operator()== or the matcher implementations don't have an explicit specialization for CString.

OTHER TIPS

Ended up being a different error. sighs It was actually catching a bug.... Google Mocks can compare CStrings just fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top