Showing posts with label Visual Studio 2012. Show all posts
Showing posts with label Visual Studio 2012. Show all posts

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




Tuesday, November 12, 2013

VS2012 and Gmock - Writing your mock classes

This video is the fifth in a series of videos that show how to use VS2012 and google test and mock. 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.
In this video I will show you how to write a mock class and then how to use the mock class in writing you unit level tests.



First you should understand what a mock class is. A Mock is fake object in the system that replaces a dependency and verifies whether the object under test interacted as expected with the dependency.

To understand this better check out the architecture of the BitcoinWallet Class.

BitcoinWallet has dependency on three other classes. BitcoinBank, BitcoinTransaction, and BitcoinCommerce. But we only want to focus our testing on BitcoinWallet. We don't want to call the implementation of BitcoinBank. BitcoinBank could have an implementation that actually calls a bank. That would be a bad thing if every time we ran our unit level tests it transferred money from one account to another. :) So we need to create a Mock to replace the real classes.


Put the mocks in place allows Bitcoin to be tested by itself and without exercising the other classes in the architecture. Here are some examples of what the mock classes look like.

#include <gmock/gmock.h>
#include "BitcoinTransaction.h"

class BitcoinTransaction_Mock : public BitcoinTransaction {
public:
MOCK_METHOD3(set,void(const string&, const string&, long));
MOCK_METHOD0(amount, int(void));
};

There are a couple things you need to watch out for when using Google Mock.

  • Only virtual methods can be Mock(ed)
  • If you don't have virtual methods check out the documentation on gmock on how to work around the problem. https://code.google.com/p/googlemock/
  • You must always put a mock method definition (MOCK_METHOD*) in a public: section of the mock class
  • Your Mock Method must match the cardinality of the real method.
  • Mocked classes do not do anything. They just pass back data.
Check my next video where I discuss how to use ON_CALL and EXPECT_CALL macros. But here is a small example of how to use the Mock classes and the ON_CALL macro.

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "BitcoinWallet.h"
#include "BitcoinBank_Mock.h"
#include "BitcoinCommerce_Mock.h"
#include "BitcoinCurrency_Mock.h"

using ::testing::Return;
using ::testing::_;

BitcoinBank* ExternalService::theBank = new BitcoinBank_Mock();
BitcoinCommerce* ExternalService::theCommerce = new BitcoinCommerce_Mock();
BitcoinCurrency* ExternalService::theCurrency = new BitcoinCurrency_Mock();

TEST(BitcoinWalletTest,updateLocalBalanceTestNoAccount) {
   BitcoinBank_Mock* bbm = new BitcoinBank_Mock();
   BitcoinWallet bw;
   ExternalService::theBank = bbm;
   ON_CALL(*bbm, changeBalance(_,_))
  .WillByDefault(Return(-1));

   ASSERT_EQ(bw.updateLocalBalance(),ActionStatus::AccountNotExist);
}