Showing posts with label gmock. Show all posts
Showing posts with label gmock. Show all posts

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

Monday, December 2, 2013

VS2012 and Gmock - Using ON_CALL and EXPECT_CALL Macros - Video #6

This is the sixth video in a series on how to use GoogleTest and GoogleMock with the VS2012 framework. Before watching this video you should make sure that you have already setup your Visual Studio solution to have a googlemock, googletest and a unit level test project. If you do not know how to do this check out the previous videos/blogs in the series. Here at "Using VS2012 and Google Test Video #1" in this blog.
In this video I will show you how to use the ON_CALL and EXPECT_CALL macros to set up default behavior for a mock object and to set expectations on method calls to the mocked object.


ON_CALL macro

This macro helps you set the behavior of the call to the method for the specified object. This is the default behavior of the object when the method is called with the specified arguments (matchers). The macros can be called as follows.
ON_CALL(mock_object, method(matchers))
    .With(multi_argument_matcher)  ?
    .WillByDefault(action);

The mactchers allows you to specify the values of the arguments that are passed to the method. This gives you the flexibility to set behavior based on parameters passed to the method. Very powerful when you are writing white box testing. The .With allows you to specify multiple matchers for the method call. The WillByDefault method allows you to specify the default action to perform when the method is called. Most of the time that is "Return".

Matchers

Matchers let developers set the default behavior based on the parameters passed into the method specified in the macro. This gives you the ability to change the behavior of the mocked object based on the parameters passed into the method. Googlemock gives several matching macros:
  • Wild Cards - 
    • “_” - Match anything
    • “A<type>()” – Match any type “int”, “char”, “Class”
  • Comparators
    • Eq(value), Ne(value), IsNull(), NoNull(), Ge(), Le(), …
  • String Matchers
    • StrEq(value), StrNe(value), ContainsRegex(string), …
Here is an example of using the ON_CALL macro to set the behavior of the method changeBalance when the first parameter is "MyAccount".
ON_CALL(*bbm, changeBalance(StrEq("MyAccount"),_))
  .WillByDefault(Return(100));
In this example any call to changeBalance with the first parameter equal to "MyAcount" will return 100. Pretty simple example, but you can see the power of being able to set behavior of a mock method in the test that is using the mock method. (High Cohesion, Low Coupling) :)

EXPECT_CALL macro

This macro is used to set the expectation of a mock method for a specific mock object. This allows you to write pinpoint white box testing for the class without having to write complex test frameworks for you specific architecture.
EXPECT_CALL(mock_object, method(matchers))
   .Times(cardinality)
   .WillOnce(action)
   .WillRepeatedly(action);

The Times, WillOnce and WillRepeatedly calls are options and can be used more than once.

Times

How many times will the method be called. If the number does not match then an assertion is set and the test will fail.

WillOnce

WillOnce specifies what we are expecting the function should do. The action that will take place. The order of WillOnce specifies the order of the expected actions. You can chain WillOnce calls together to show that a method is called several times with different behaviors. You can finish your change with WillRepeatedly to specify that the method can be called more times with specific action.

using ::testing::Return;

EXPECT_CALL(turtle, GetY())
   .WillOnce(Return(100))
   .WillOnce(Return(200))
   .WillRepeatedly(Return(300));

There are several good resources availble on the google mock site:
I hope this helps you understand googlemock a bit more and helps you move to a more Test Driven Development model. Happy developing.

DWP