I have a function I want to mock:
class Base{
public:
void virtual func() { sleep(3); }
};
My mock would look like:
class Mock: public Base{
public:
MOCK_METHOD(void, func, (), (override));
};
And the test would look like:
TEST(TestMockSanity, AbleToMock) {
Mock mock;
EXPECT_CALL(mock, func());
}
however, I have 2 issues:
- something silly - error: ‘class testing::internal::TypedExpectation<void()>’ has no member named ‘TIMES’
- Important - How do I change the implemntation of
func
tosleep(0)
?
./real.hpp
class Real {
public:
Real(){};
virtual ~Real(){};
virtual int run();
virtual int dont_run();
};
./real.cpp
#include "real.hpp"
int Real::run() { return 0; };
int Real::dont_run() { return 1; };
./meson.build
project(
'gtest',
'cpp',
default_options : [
'cpp_std=c 14',
],
)
sources = files('main.cpp', 'real.cpp')
executable(
'real',
sources,
install : false,
)
executable(
'mock',
files('real.cpp', 'mock.cpp'),
dependencies: [dependency('gtest'), dependency('gmock')],
install : false,
)
./mock.cpp
#include "gmock/gmock.h" // MOCK_METHOD
#include "gtest/gtest.h" // TEST
#include "real.hpp"
class MockReal : public Real {
public:
MOCK_METHOD(int, run, (), (override));
MOCK_METHOD(int, dont_run, (), (override));
void delegate(){
ON_CALL(*this, run).WillByDefault([this](){
return 1;
});
ON_CALL(*this, dont_run).WillByDefault([this](){
return 2;
});
}
};
TEST(TestMockSanity, AbleToMock) {
MockReal mock;
EXPECT_EQ(0, mock.run());
EXPECT_EQ(1, mock.dont_run());
mock.delegate();
EXPECT_EQ(1, mock.run());
EXPECT_EQ(2, mock.dont_run());
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
./main.cpp
#include "real.hpp"
int main() {
Real r;
r.run();
return 0;
}
subprojects/gtest.wrap
[wrap-file]
directory = googletest-release-1.11.0
source_url = https://github.com/google/googletest/archive/release-1.11.0.tar.gz
source_filename = gtest-1.11.0.tar.gz
source_hash = b4870bf121ff7795ba20d20bcdd8627b8e088f2d1dab299a031c1034eddc93d5
patch_filename = gtest_1.11.0-2_patch.zip
patch_url = https://wrapdb.mesonbuild.com/v2/gtest_1.11.0-2/get_patch
patch_hash = 764530d812ac161c9eab02a8cfaec67c871fcfc5548e29fd3d488070913d4e94
[provide]
gtest = gtest_dep
gtest_main = gtest_main_dep
gmock = gmock_dep
gmock_main = gmock_main_dep
And I get after running:
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from TestMockSanity
[ RUN ] TestMockSanity.AbleToMock
GMOCK WARNING:
Uninteresting mock function call - returning default value.
Function call: run()
Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md#knowing-when-to-expect for details.
GMOCK WARNING:
Uninteresting mock function call - returning default value.
Function call: dont_run()
Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md#knowing-when-to-expect for details.
../mock.cpp:24: Failure
Expected equality of these values:
1
mock.dont_run()
Which is: 0
GMOCK WARNING:
Uninteresting mock function call - taking default action specified at:
../mock.cpp:11:
Function call: run()
Returns: 1
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md#knowing-when-to-expect for details.
GMOCK WARNING:
Uninteresting mock function call - taking default action specified at:
../mock.cpp:14:
Function call: dont_run()
Returns: 2
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md#knowing-when-to-expect for details.
[ FAILED ] TestMockSanity.AbleToMock (0 ms)
[----------] 1 test from TestMockSanity (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] TestMockSanity.AbleToMock
1 FAILED TEST
CodePudding user response:
Issue 1: You have to say how often you expect the call of func()
in EXPECT_CALL
. For example like:
EXPECT_CALL(mock, func()).Times(1);
Issue 2: You can use Googletests Invoke
to do an action on the call.
CodePudding user response:
I've got something like this in my ServerMock class that also has MOCK_METHODs
ServerMock()
{
ON_CALL(*this, Stop).WillByDefault([this] { StopImpl(); });
}
void StopImpl() const
{
...
}
Hope that helps