views:

208

answers:

2

Hey All,

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

A: 

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.

Steve Guidi
The "Some Value" is a constant within 'myMethod'. The comparison is between the "Some Value" I am providing the mock and the "Some Value" that is produced in the method. Also, CString does override the operator()== so I would assume that the Eq(v) function could at least utilize this. Thanks for the input!
Adam Driscoll
I see. If "Some Value" is inaccessible, then you might want to try the `ResultOf` matcher, which executes a predicate of your choice against the function's input parameter. It seems like overkill for string matching, but may provide some clues as to why the other matchers are not working.
Steve Guidi
A: 

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

Adam Driscoll