https://github.com/shensunbo/google_test_insight
- 什么样的类是可以使用gtest测试的
- 类的依赖采用依赖注入的方式引入
- 依赖在类中是指针或引用,并且可以通过接口设置
-
编写类时如何注意适配gmock
- 什么样的类无法使用gtest测试
- 不使用依赖注入的方式引入的依赖项无法被mock
- 类内直接使用依赖项的实例作为成员时,gmock不能生效,必须与EXPECT_CALL中的mock对象指向的是同一个对象,EXPECT_CALL才会被满足
- When a mock is destructed, gMock will automatically check whether all expectations on it have been satisfied.
- By creating an object of type InSequence, all expectations in its scope are put into a sequence and have to occur sequentially. Since we are just relying on the constructor and destructor of this object to do the actual work, its name is really irrelevant.
1 2 3 4 5 6 7 8 9 10 11 12 13
using ::testing::InSequence; ... TEST(FooTest, DrawsLineSegment) { ... { InSequence seq; EXPECT_CALL(turtle, PenDown()); EXPECT_CALL(turtle, Forward(100)); EXPECT_CALL(turtle, PenUp()); } Foo(); }
- You must always put a mock method definition (MOCK_METHOD) in a public: section of the mock class, regardless of the method being mocked being public, protected, or private in the base class.
- if you don’t mock all versions of the overloaded method, the compiler will give you a warning about some methods in the base class being hidden. To fix that, use using to bring them in scope:
using Foo::Add;
- Delegating Calls to a Fake, 可以使用ON_CALL将mock的成员绑定到concrete的成员上,
- mock non virtual function: use template
- Mocking Free Functions: encapsulate to a class
高频问题
- If a mock method has no EXPECT_CALL spec but is called, we say that it’s an “uninteresting call”. Currently, an uninteresting call will also by default cause gMock to print a warning.
- 使用
NiceMock
来创建mock对象可以消除这个警告,NiceMock<MockFoo>
- 使用
StrictMock
可以让这个警告变成错误
- 使用