-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Source Code: https://github.com/vishalbhardwaj26/UnitTestsUsingFakes
Person is a class using Logger to log information underneath. We are pushing logger as dependency injection to Person. Let’s test Person class with mocking the dependencies using google framework.
-
Install GoogleTest
-
Install gMock
-
If you got error: Unexpected end of file while looking for precompiled header
Set the Create/Use Precompiled Header property of source files to Not Using Precompiled Headers. To set this compiler option, follow these steps: 1. In the Solution Explorer pane of the project, right-click the project name, and then click Properties. 2. In the left pane, click the C/C++ folder. 3. Click the Precompiled Headers node. 4. In the right pane, click Create/Use Precompiled Header, and then click Not Using Precompiled Headers.
- Mock the dependency
- Implement Logger as Mock_Logger.h
- Mock the required functions.
- Write tests in test class using mocked dependencies and expectations
- Write Tests in MockLogger.cc with required expectations
- Implement main function to run all the available tests in gtest framework.
`#include (gtest\gtest.h)
int main(int ac, char* av[]) {
testing::InitGoogleTest(&ac, av);
return RUN_ALL_TESTS();
}`
-
If you want to see test result on output on every build:
- Paste this in post build event command line:
"$(TargetDir)$(TargetFileName)"
- Paste this in post build event command line:
"$(TargetDir)$(TargetFileName)"
-
If you want to run tests using Test explorer, you need to add plugin Tools->Extension and Updates->Install “Google TestRunner for Visual Studio 2015”
- Add fake asmble for creating stubs