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);
.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
No comments:
Post a Comment