When I researched Gmock from Google, I've installed and built the project work well so far. But I have some concern about mocking a function. Now I have the following files:
<1> myGtest.h
#ifndef MYGTEST_H_
#define MYGTEST_H_
int test(int);
int function(int);
#endif /* MYGTEST_H_ */
<2> src_code.cpp
#include <stdio.h>
#include "myGtest.h"
int test(int a) {
printf("NOT overridden!\n");
return a;
}
int function(int a){
int x = test(a);
if(x == 0)
return 99;
else
return 0;
}
<3> myGtest_dummy.h
#ifndef MYGTEST_DUMMY_H_
#define MYGTEST_DUMMY_H_
#include "gmock/gmock.h"
#include "../myGtest/myGtest.h"
class myGtestMock
{
public:
myGtestMock(){};
~myGtestMock(){};
MOCK_METHOD1(test, int(int));
};
#endif /* MYGTEST_DUMMY_H_ */
<4> test_program.cpp
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "src/myGtest/myGtest.h"
#include "src/dummy/myGtest_dummy.h"
using testing::_;
using testing::Return;
using testing::InSequence;
using ::testing::AtLeast;
extern int function(int a);
extern int test(int a);
class BTest:public testing::Test{
public:
myGtestMock mock_test;
int __wrap_test(int a);
};
int BTest::__wrap_test(int a){
printf("overridden!\n");
return a;
}
TEST_F(BTest, CallMockTest) {
EXPECT_CALL(mock_test, test(0))
.WillOnce(Invoke(this, &BTest::__wrap_test));
function(99);
}
int main(int argc, char *argv[]) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Could you help me explain that: How can I mock the function int test(int)
? I'd like to expect that once TEST_F(BTest, CallMockTest)
is executed, the program call function(99);
. Then my mocking function int __wrap_test(int)
will be called instead of int test(int)
.
Thanks so much for the answer.