-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpartialMocking.cpp
More file actions
38 lines (30 loc) · 928 Bytes
/
partialMocking.cpp
File metadata and controls
38 lines (30 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <gtest/gtest.h>
#include <gmock/gmock.h>
// https://godbolt.org/z/jqEhvo13Y
class Concrete {
public:
int NonMockedMethod() {
return 42;
}
virtual int MockedMethod() {
return 43;
}
};
class MockConcrete : public Concrete {
public:
MOCK_METHOD(int, MockedMethod, (), (override));
MockConcrete() {
// Call the real implementation by default
ON_CALL(*this, MockedMethod())
.WillByDefault([this]() { return Concrete::MockedMethod(); });
}
};
TEST(MockConcreteTest, CanMockConcrete) {
MockConcrete obj;
// When we call NonMockedMethod, we call the real implementation
EXPECT_EQ(42, obj.NonMockedMethod());
EXPECT_EQ(43, obj.MockedMethod());
// We can still override MockedMethod to return a different value
EXPECT_CALL(obj, MockedMethod()).WillOnce(::testing::Return(44));
EXPECT_EQ(44, obj.MockedMethod());
}