Friday, January 24, 2014

Google Test and Google Mock Using Matchers to change default behavior

This blog is in a series of blogs that talks about how to use Google Test and Google Mock in the VS2012 environment. This is the 8th blog in that series.

Matchers

—Matchers let developers set the default behavior based on the parameters passed into the macro. —Google Mock gives several matching macros

Wildcard

"_"argument can be any value of the correct type.
"A<type>() or An<type>()"argument can be any value of type type.

Generic Comparison

Eq(value) or valueargument == value
Ge(value)argument >= value
Gt(value)argument > value
Le(value)argument <= value
Lt(value)argument < value
Ne(value)argument != value
IsNull()argument is a NULL pointer (raw or smart).
NotNull()argument is a non-null pointer (raw or smart).
Ref(variable)argument is a reference to variable.
TypedEq<type>(value)argument has type type and is equal to value. You may need to use this instead of Eq(value) when the mock function is overloaded.

String Matchers

The argument can be either a C string or a C++ string object:
ContainsRegex(string)argument matches the given regular expression.
EndsWith(suffix)argument ends with string suffix.
HasSubstr(string)argument contains string as a sub-string.
MatchesRegex(string)argument matches the given regular expression with the match starting at the first character and ending at the last character.
StartsWith(prefix)argument starts with string prefix.
StrCaseEq(string)argument is equal to string, ignoring case.
StrCaseNe(string)argument is not equal to string, ignoring case.
StrEq(string)argument is equal to string.
StrNe(string)argument is not equal to string.
ContainsRegex() and MatchesRegex() use the regular expression syntax defined hereStrCaseEq()StrCaseNe()StrEq(), and StrNe()work for wide strings as well.

—Class Tested -

If you want to test for specific value returned from a method.
EXPECT_THAT(value, matcher)
ASSERT_THAT(value, matcher)
Here is an example
#include "gmock/gmock.h"

using ::testing::AllOf;
using ::testing::Ge;
using ::testing::Le;
using ::testing::MatchesRegex;
using ::testing::StartsWith;
...

  EXPECT_THAT(bankMock->getAccount(), StartsWith("MyAcc"));
  EXPECT_THAT(bankMock->getAccount(), MatchesRegex("MyAccount \\d+"));
  ASSERT_THAT(bankMock->getBalance(), AllOf(Ge(500), Le(1000)));

—Mocked Class -

If you want the behavior of a MOCKED class to behave based on the values of parameters passed to a method.
ON_CALL(mock_object, method(matchers))
EXPECT_CALL(mock_object, method(matchers) ...
Set up default behavior for you MOCK class in your test fixture SetUp() Method
virtual void SetUp() {

ExternalService::theBank = bankMock = new BitcoinBank_Mock();
ON_CALL(*bankMock,changeBalance(_,_))
   .WillByDefault(Return(100));

}
Override mock behavior in specific test cases.
TEST_F(BitcoinWallet_ULT,updateLocalBalanceTest) {

ON_CALL(*bankMock, changeBalance(StrEq("MyAccount"),_))
.WillByDefault(Return(100));

}
This gives me the ability to set up default behavior in the SetUp of the fixture and override it in the test case run of the Test.

I hope this helps clarify more about Matchers. There is actually even more information available at https://code.google.com/p/googlemock/wiki/CheatSheet#Matcher
DWP