Using GoogleMock and VS2012 can be tricky. This video shows how to get things started so you aren't wasting time trying to figure out include paths, Pre-Processor directives and project configuration.
This video does not show you details of gmock just how to get it setup and begin using in your project. The next video will go into more details on how to use gmock effectively.
- Compile googlemock into a library.
- Create a new project of static library: Add->New Project->Win32 Project->Static Library (no precompiled header)
- Add include path <gmock_dir>, <gmock_dir>/include, <gtest_dir>, and <gtest_dir>/include
- Add source file <gmock_dir>/src/gtest_all.cc and gtest_main.cc
- Change the
pre-processor rules to include the definition.
_VARIADIC_MAX
=10 - Add references to Your Unit Test
project.
- Add the gmock project as an external reference.
- Add the gmock include directories to Include Path <gmock_dir> and <gmock_dir>/include
BitcoinTransactionMock.h
#include <gmock/gmock.h>
#include "BitcoinTransaction.h"
class BitcoinTransactionMock : public BitcoinTransaction {
public:
MOCK_METHOD3(set,void(const string&, const string&, long));
MOCK_METHOD0(amount, int(void));
};
Now that I have a Mock(ed) class I can use the mocked class in my tests.
#include <gtest/gtest.h>
#include "BitcoinTransactionMock.h"
#include <gmock/gmock.h>
using ::testing::Return;
TEST(BitcoinTransactionMock,setGoodValues) {
BitcoinTransactionMock* bt = new BitcoinTransactionMock();
ASSERT_NE(bt, nullptr);
ON_CALL(*bt, amount())
.WillByDefault(Return(100));
bt->set("MyAccount","YourAccount", 1000);
ASSERT_EQ(bt->amount(), 100);
}
In this example The Macro ON_CALL is used to set the return value of the call to amount.
The next video and blog will go into more depth on how to use gmock and the benefits of isolation in unit level testing.
DWP.
No comments:
Post a Comment