C/C++ Test Framework - Google Test & Google Mock

Edit

Google Test,Google Mock以下简称gtest,gmock。
在接触gtest,gmock之前,测试C/C++ code使用UnitTest++。这是一个很简洁的框架,上手很快。参看另一篇博文UnitTest++简介。测试相关的功能够用,但是没有mock库。这带来的问题是:

  1. 测试遗留代码的时候,需要自行fake相关代码。这个在遗留系统很庞大时,要颇费心力。而且过多涉及细节,导致测试极不稳定,系统代码任意的演进,都会导致大堆的测试失败,甚至测试无法进行。
  2. 测试case之间无法很好的解耦。结果同样是测试不够稳定。术语是测试代码很“脆弱”。

Java,Python,JavaScript都有自己的mock库。Python的Mock类,Java的Mockito/PowerMock,JavaScript的Sinon。于是,在网上搜索了一下C/C++的Mock库,于是看到了gtest,gmock。然后就有了这一篇。

简介

不用去网上费心找教程,两个项目的文档都非常棒。入口统一在gtest GitHub项目主页上。而且该文档不仅很好的介绍了gtest,gmock的用法,其中还涉及了很多TDD或者UnitTest的真知灼见,很值得读一读。
要使用gtest非常简单:编译出gtest,gmock,再链入你的测试程序。

编译gtest/gmock

gtest,gmock均用cmake来管理跨平台,先用cmake来生成Makefile。用命令cmake -G "Unix Makefiles" /path/to/CMakeList.txt

Makefile

然后按照下面编写Makefile。注意gmock_main是一个main函数来调用所有的test case,省得自己写main函数了。

CC = gcc
CPP = g++
LINK = g++
CFLAGS = -g -Wall -Werror -Wextra -std=gnu99
CPPFLAGS = -g -Wall -Werror -Wextra
LIBS = -L./lib -lgtest -lgmock -lgmock_main -lpthread
C__SOURCES = $(wildcard *.c)
CPPSOURCES = $(wildcard *.cpp)
OBJECTS = $(patsubst %.c, %.o, $(C__SOURCES)) $(patsubst %.cpp, %.o, $(CPPSOURCES))
TARGET = test_exe
first: all
%.o: %.c
$(CC) $(INCLUDES) -c $(CFLAGS) -o $@ $<
%.o: %.cpp
$(CPP) $(INCLUDES) -c $(CPPFLAGS) -o $@ $<
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(LINK) $(CPPFLAGS) $(LIBS) -o $(TARGET) $(OBJECTS)
.PHONY : clean
clean:
rm -f $(TARGET) $(OBJECTS)

Terms

MeaningGoogle Test TermISTQB Term
Exercise a particular program path with specific input values and verify the resultsTEST()Test Case
A set of several tests related to one componentTest CaseTest Suite

Test

#include "gtest/gtest.h"
#include "gmock/gmock.h"
using ::testing::Return;
using ::testing::Test;
using ::testing::_;
using ::testing::AtLeast;
TEST(TestCaseName, should_this_test_do)
{
...
EXPECT_STREQ("{}", str);
}

中间的那堆namespace都是gtest/gmock库里定义的matcher宏或者各种有用的宏。

Test Fixture

在测试有重复的时候,就要用到Test Fixture了,也就是setUp / tearDown。

class QueueTest : public ::testing::Test {
protected:
virtual void SetUp() {
q1_.Enqueue(1);
q2_.Enqueue(2);
q2_.Enqueue(3);
}
// virtual void TearDown() {}
Queue<int> q0_;
Queue<int> q1_;
Queue<int> q2_;
};
TEST_F(QueueTest, IsEmptyInitially) {
EXPECT_EQ(0, q0_.size());
}
TEST_F(QueueTest, DequeueWorks) {
int* n = q0_.Dequeue();
EXPECT_EQ(NULL, n);
n = q1_.Dequeue();
ASSERT_TRUE(n != NULL);
EXPECT_EQ(1, *n);
EXPECT_EQ(0, q1_.size());
delete n;
n = q2_.Dequeue();
ASSERT_TRUE(n != NULL);
EXPECT_EQ(2, *n);
EXPECT_EQ(1, q2_.size());
delete n;
}

constructor/destructor vs. SetUp/TearDown

When you need to write per-test set-up and tear-down logic, you have the choice between using the test fixture constructor/destructor or SetUp()/TearDown(). The former is usually preferred, as it has the following benefits:

  • By initializing a member variable in the constructor, we have the option to make it const, which helps prevent accidental changes to its value and makes the tests more obviously correct.
  • In case we need to subclass the test fixture class, the subclass’ constructor is guaranteed to call the base class’ constructor first, and the subclass’ destructor is guaranteed to call the base class’ destructor afterward. With SetUp()/TearDown(), a subclass may make the mistake of forgetting to call the base class’ SetUp()/TearDown() or call them at the wrong moment.

Benefit for using SetUp/TearDown:

  • If the tear-down operation could throw an exception, you must use TearDown() as opposed to the destructor, as throwing in a destructor leads to undefined behavior and usually will kill your program right away. Note that many standard libraries (like STL) may throw when exceptions are enabled in the compiler. Therefore you should prefer TearDown() if you want to write portable tests that work with or without exceptions.
  • The assertion macros throw an exception when flag –gtest_throw_on_failure is specified. Therefore, you shouldn’t use Google Test assertions in a destructor if you plan to run your tests with this flag.
  • In a constructor or destructor, you cannot make a virtual function call on this object. (You can call a method declared as virtual, but it will be statically bound.) Therefore, if you need to call a method that will be overriden in a derived class, you have to use SetUp()/TearDown().

简言之,在逻辑上,这两组的作用相同,都是每个测试之前之后会做一些处理工作。Constructor/Destructor的好处是提供了继承。setUp/tearDown的好处是可以处理exception,这是不能放在析构函数里的。

SetUpTestCase / TearDownTestCase

Test Case级别的SetUp/TearDown

class FooTest : public ::testing::Test {
protected:
// Per-test-case set-up.
// Called before the first test in this test case.
// Can be omitted if not needed.
static void SetUpTestCase() {
shared_resource_ = new ...;
}
// Per-test-case tear-down.
// Called after the last test in this test case.
// Can be omitted if not needed.
static void TearDownTestCase() {
delete shared_resource_;
shared_resource_ = NULL;
}
// You can define per-test set-up and tear-down logic as usual.
virtual void SetUp() { ... }
virtual void TearDown() { ... }
// Some expensive resource shared by all tests.
static T* shared_resource_;
};

SetUp/TearDown Environment

  • First, you subclass the ::testing::Environment class to define a test environment, which knows how to set-up and tear-down:
  • Then, you register an instance of your environment class with Google Test by calling the ::testing::AddGlobalTestEnvironment() function:
    Now, when RUN_ALL_TESTS() is called, it first calls the SetUp() method of the environment object, then runs the tests if there was no fatal failures, and finally calls TearDown() of the environment object.
  • It’s OK to register multiple environment objects. In this case, their SetUp() will be called in the order they are registered, and their TearDown() will be called in the reverse order.
  • Note that Google Test takes ownership of the registered environment objects. Therefore do not delete them by yourself.
class Environment {
public:
virtual ~Environment() {}
// Override this to define how to set up the environment.
virtual void SetUp() {}
// Override this to define how to tear down the environment.
virtual void TearDown() {}
};
Environment* AddGlobalTestEnvironment(Environment* env);

断言

有两种断言EXPECT_xxx和ASSERT_xxx。前者会让测试终止,后者不会,只会让测试fail。

gmock

之所以要切到gtest,唯一的原因就是gmock,所以要专开一章重点介绍一下。所有内容均来自于官方文档。内容深度由浅入深,依次如下:

最后还有参考手册:

简介

Google C++ Mocking Framework (or Google Mock for short) is a library (sometimes we also call it a “framework” to make it sound cool) for creating mock classes and using them. It does to C++ what jMock and EasyMock do to Java.

何为Mock?

Mocks are objects pre-programmed with expectations, which form a specification of the calls they are expected to receive.

相应的还有Fake和Stub

Fake objects have working implementations, but usually take some shortcut (perhaps to make the operations less expensive), which makes them not suitable for production. An in-memory file system would be an example of a fake.

gmock的文档里只提到了Fake,从Martin Fowler的文章Mocks Aren’t Stubs中摘录如下:

Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test.

三者作用相同,都是模拟系统其他部分的功能,达到代码隔离的效果,方便测试。但是Mock的特点是更OO化,也符合TDD或者BDD的思想——针对一个object设置期待,再对齐verify。

Mocks vs. Stubs - from Martin Fowler

In order to use state verification on the stub, I need to make some extra methods on the stub to help with verification. As a result the stub implements MailService but adds extra test methods.

Mock objects always use behavior verification, a stub can go either way. Meszaros refers to stubs that use behavior verification as a Test Spy. The difference is in how exactly the double runs and verifies and I’ll leave that for you to explore on your own.

Getting Started

Class to Mock

class Turtle {
...
virtual ~Turtle() {}
virtual void PenUp() = 0;
virtual void PenDown() = 0;
virtual void Forward(int distance) = 0;
};

Mock class

#include "gmock/gmock.h" // Brings in Google Mock.
class MockTurtle : public Turtle {
public:
...
MOCK_METHOD0(PenUp, void());
MOCK_METHOD0(PenDown, void());
MOCK_METHOD1(Forward, void(int distance));
};

针对待Mock的Turtle class要注意的是:

Note that the destructor of Turtle must be virtual, as is the case for all classes you intend to inherit from - otherwise the destructor of the derived class will not be called when you delete an object through a base pointer, and you’ll get corrupted program states like memory leaks.

Use it

#include "path/to/mock-turtle.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using ::testing::AtLeast; // #1
TEST(PainterTest, CanDrawSomething) {
MockTurtle turtle; // #2
EXPECT_CALL(turtle, PenDown()) // #3
.Times(AtLeast(1));
Painter painter(&turtle); // #4
EXPECT_TRUE(painter.DrawCircle(0, 0, 10));
} // #5
int main(int argc, char** argv) {
// The following line must be executed to initialize Google Mock
// (and Google Test) before running the tests.
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}

如果你愿意的话,可以自己写main,如果你想偷懒,记得在Makefile里link gmock_main。
按Google的说法,gmock也可以和其他测试框架兼容,但总感觉挺悬的。

一些有用的工具

Marcher
EXPECT_CALL(turtle, Forward(_));
     以任意参数调用Forward
EXPECT_CALL(turtle, Forward(Ge(100)));
     以大于100的整数调用Forward

Cardinalities: How Many Times Will It Be Called?

  • If neither WillOnce() nor WillRepeatedly() is in the EXPECT_CALL(), the inferred cardinality is Times(1).
  • If there are n WillOnce()’s but no WillRepeatedly(), where n >= 1, the cardinality is Times(n).
  • If there are n WillOnce()’s and one WillRepeatedly(), where n >= 0, the cardinality is Times(AtLeast(n)).
EXPECT_CALL(mockObj, func())
.Times(AtLeast(1))
.WillOnce(Return(123))
.WillRepeatedly(Return(456))

上面这段代码要求func函数至少运行一次,第一次返回123,之后每次返回456。

Important note: The EXPECT_CALL() statement evaluates the action clause only once, even though the action may be performed many times. Therefore you must be careful about side effects. The following may not do what you want:

int n = 100;
EXPECT_CALL(turtle, GetX())
.Times(4)
.WillRepeatedly(Return(n++));

因为Return是宏,所以只会替换一次,所以不管GetX调用几次,返回都是101,而不是101,102,103,…

All Expectations Are Sticky
所谓的sticky就是EXPECT_CALL总是生效的,除非你显示的将其失效。如下,所有的EXPECT_CALL都会生效,那么最后一个会覆盖前面所有的,也就是GetX总会返回10。

using ::testing::Return;
...
for (int i = n; i > 0; i--) {
EXPECT_CALL(turtle, GetX())
.WillOnce(Return(10*i));
}

如果希望他返回,30,20,10,…,应该这么写

using ::testing::Return;
...
for (int i = n; i > 0; i--) {
EXPECT_CALL(turtle, GetX())
.WillOnce(Return(10*i))
.RetiresOnSaturation();
}

RetireOnSaturation就是显示的让其失效。还有一个办法:

using ::testing::InSequence;
using ::testing::Return;
...
{
InSequence s;
for (int i = 1; i <= n; i++) {
EXPECT_CALL(turtle, GetX())
.WillOnce(Return(10*i))
.RetiresOnSaturation();
}
}

因为InSequence的关系,在前面的EXPECT_CALL生效以后,就会自动失效,所以不会产生覆盖的效果。
Ordered vs Unordered Calls

using ::testing::InSequence;...
TEST(FooTest, DrawsLineSegment) {
...
{
InSequence dummy;
EXPECT_CALL(turtle, PenDown());
EXPECT_CALL(turtle, Forward(100));
EXPECT_CALL(turtle, PenUp());
}
Foo();
}

Expecting Partially Ordered Calls
Google Mock allows you to impose an arbitrary DAG (directed acyclic graph) on the calls. One way to express the DAG is to
use the After clause of EXPECT_CALL .

using ::testing::Sequence;
Sequence s1, s2;
EXPECT_CALL(foo, A())
.InSequence(s1, s2);
EXPECT_CALL(bar, B())
.InSequence(s1);
EXPECT_CALL(bar, C())
.InSequence(s2);
EXPECT_CALL(foo, D())
.InSequence(s2);

specifies the following DAG (where s1 is A -> B , and s2 is A -> C -> D ):

+---> B
|
A ---|
|
+---> C ---> D

Uninteresting Calls
这是gmock报的warning。当针对某一个待测函数设置了EXPECT spec,却并没有调用的时候,就会报这个warning。此时gmock认为你对该函数并没有兴趣,所以就不需要这个EXPECT spec。当然你可以选择忽略这个warning,但我认为出这个warning的时候,多半是test漏写了什么。

Returning Live Values from Mock Methods

using testing::ByRef;
using testing::Return;
class MockFoo : public Foo {
public:
MOCK_METHOD0(GetValue, int());
};
...
int x = 0;
MockFoo foo;
EXPECT_CALL(foo, GetValue())
// .WillRepeatedly(Return(ByRef(x))); X error
.WillRepeatedly(ReturnPointee(x));
x = 42;
EXPECT_EQ(42, foo.GetValue());

一些测试case

写Unit Test并不像想像的那么简单,并不是调用了框架,针对每个函数写test case就可以。按我目前的理解有以下几种挑战:

  1. Test Case如何解耦。不要有重复测试(overlap)。
    例如:在写A函数的时候,写了测试testA,B函数会调用A函数,那么在写完A之后写B的测试testB时,是否要将A mock/fake/stub掉? 如果不将A函数Fake掉,则testA和testB之间就是有overlap。我认为这之间可以有取舍,最佳状态应当是此时,将testA删除,只保留testB。但仍应根据具体情况而定。

  2. 如何针对依赖关系进行mock化。
    例如出现这样的语句: B = new A,则类B依赖于类A。但类A并没有必要编译进test。因为一旦加入A,则势必会引入更多依赖关系,而导致test编译崩溃。依赖关系的解决无穷无尽。在做Android的单元测试时,可以用PowerMock取代Mockito来Mock构造函数,将构造函数Fake化成类似工厂函数,返回类实例。具体参看这篇博文Android单元测试
    但实际上,按照现在的理解,其实Mock构造函数是不可取的,首先造成被测代码spec不清晰,试想一个构造函数怎么会返回另一个类的实例。其次,在C++中很难做到Mock构造函数。好的做法应当是运用Dependency Injection。例如:

class A
{}
class B
{
void func()
{
A* a = new A;
}
}

类B应当改写为:

class B
{
void func(A*)
{
...
}
}

将类A指针传入,解决dependency的问题。

  1. 如何能够让测试稳定,在任意环境下均返回同样的测试结果。
    这个一般涉及测试环境的影响。例如调用网络相关的功能,在没有网络的环境就没法进行。再例如测试时操作真实具体文件,则该文件被测试外人为或代码修改,则测试可能就会莫名失败。
    针对这些情况,我们应当在测试中尽量避免。例如前者,我们应当对网络接口进行Mock化,后者应当在测试的setUp和tearDown中生成虚假文件用于测试,并在测试完成时做清理。

  2. 会因为很小的被测代码改动,而导致大面积测试失败,甚至测试崩溃。
    这个就是gmock文档中提到的要针对接口编程,针对接口测试。Robert C·Martin在《敏捷软件开发-原则、模式与实践》一书中有提出:所有的代码都应依赖于抽象接口。因为抽象接口是经过抽象的,相对具体的实现代码较为稳定。而被依赖的代码应该尽可能保持稳定,这样基于之上的代码才不会因为依赖的改动而改动。

下面列出几种我在实际写test case时遇到的情况,在gmock中的解决方案。

按照函数参数返回结果 - Fake

例如:

class A
{
virtual int func(int a, int b);
}
EXPECT_CALL(mockA, func())
.WillRepeatedly(a+b);

gmock中可以这样做:Using Functions/Methods/Functors as Actions

using ::testing::_;
using ::testing::Invoke;
class MockFoo : public Foo {
public:
MOCK_METHOD2(Sum, int(int x, int y));
MOCK_METHOD1(ComplexJob, bool(int x));
};
int CalculateSum(int x, int y) { return x + y; }
class Helper {
public:
bool ComplexJob(int x);
};
...
MockFoo foo;
Helper helper;
EXPECT_CALL(foo, Sum(_, _))
.WillOnce(Invoke(CalculateSum));
EXPECT_CALL(foo, ComplexJob(_))
.WillOnce(Invoke(&helper, &Helper::ComplexJob));
foo.Sum(5, 6); // Invokes CalculateSum(5, 6).
foo.ComplexJob(10); // Invokes helper.ComplexJob(10);

Mock non-virtual函数

// A simple packet stream class. None of its members is virtual.
class ConcretePacketStream {
public:
void AppendPacket(Packet* new_packet);
const Packet* GetPacket(size_t packet_number) const;
size_t NumberOfPackets() const;
...
};
// A mock packet stream class. It inherits from no other, but defines
// GetPacket() and NumberOfPackets().
class MockPacketStream {
public:
MOCK_CONST_METHOD1(GetPacket, const Packet*(size_t packet_number));
MOCK_CONST_METHOD0(NumberOfPackets, size_t());
...
}
template <class PacketStream>
void CreateConnection(PacketStream* stream) { ... }
template <class PacketStream>
class PacketReader {
public:
void ReadPackets(PacketStream* stream, size_t packet_num);
};
MockPacketStream mock_stream;
EXPECT_CALL(mock_stream, ...)...;
.. set more expectations on mock_stream ...
PacketReader<MockPacketStream> reader(&mock_stream);
... exercise reader ...

为什么要这么做?
因为只能这么做。普通的mock,要通过继承被测试类,并重写virtual函数来实现。而上面的ConcretePacketStream和MockPacketStream并任何没有关系,也就是说,如果传入后者的指针,不用reinterpret_cast是不能转成前者的指针的。
所以想一个变通的办法,用模板类来定义被测代码,在测试时传入mock类,在生产时,传入真实类。

Mocking Side Effects

EXPECT_CALL(mutator, MutateInt(_))
.WillOnce(DoAll(SetArgPointee<0>(5), Return(true)));
EXPECT_CALL(mutator, Mutate(NotNull(), 5))
.WillOnce(SetArrayArgument<0>(values, values + 5));

第一个将MutateInt第一个参数指针指向的int,设为5,并返回true。
第二个将values数组的[0,5)拷贝到参数1指向的地址。
如果仍需要返回,则用DoAll,如下:

EXPECT_CALL(mutator, MutateInt(_))
.WillOnce(DoAll(SetArgPointee<0>(5),
Return(true)));

Selecting an Action’s Arguments

using ::testing::_;
using ::testing::Invoke;
bool MyIsVisibleInQuadrant1(bool visible, const string& name, int x, int y,
const map<pair<int, int>, double>& weight,
double min_weight, double max_wight) {
return IsVisibleInQuadrant1(visible, x, y);
}.
..
EXPECT_CALL(mock, Foo(_, _, _, _, _, _, _))
.WillOnce(Invoke(MyIsVisibleInQuadrant1)); // Now it works.

定义自己的adaptor MyIsVisibleInQuadrant1,或者用gmock提供的方法优雅的解决。

using ::testing::_;
using ::testing::Invoke;
using ::testing::WithArgs;
...
EXPECT_CALL(mock, Foo(_, _, _, _, _, _, _))
.WillOnce(WithArgs<0, 2, 3>(Invoke(IsVisibleInQuadrant1)));
// No need to define your own adaptor.

Mocking Private or Protected Methods

class Foo {
public:
...
virtual bool Transform(Gadget* g) = 0;
protected:
virtual void Resume();
private:
virtual int GetTimeOut();
};
class MockFoo : public Foo {
public:
...
MOCK_METHOD1(Transform, bool(Gadget* g));
// The following must be in the public section, even though the
// methods are protected or private in the base class.
MOCK_METHOD0(Resume, void());
MOCK_METHOD0(GetTimeOut, int());
};

C++ allows a subclass to specify a different access level than the base class on a virtual function.

Misc

Keep in mind that one doesn’t have to verify more than one property in one test. In fact, it’s a good style to verify only one
thing in one test. If you do that, a bug will likely break only one or two tests instead of dozens

When it’s being destroyed, your friendly mock object will automatically verify that all expectations on it have been satisfied,
and will generate Google Test failures if not.

Currently these are only platforms that support the pthreads library (this includes Linux and Mac).

加上命令行参数–gmock_verbose=info可以显示所有EXPECT_CALL的具体调用情况。

Some useful tips in gtest

Selecting Tests

If you set the GTEST_FILTER environment variable or the –gtest_filter flag to a filter string, Google Test will only run the tests whose full names (in the form of TestCaseName.TestName) match the filter.
The format of a filter is a ‘:’-separated list of wildcard patterns (called the positive patterns) optionally followed by a ‘-’ and another ‘:’-separated pattern list (called the negative patterns).

  • ./foo_test Has no flag, and thus runs all its tests.
  • ./foo_test –gtest_filter=* Also runs everything, due to the single match-everything * value.
  • ./foo_test –gtest_filter=FooTest.* Runs everything in test case FooTest.
  • ./foo_test –gtest_filter=Null:Constructor Runs any test whose full name contains either “Null” or “Constructor”.
  • ./foo_test –gtest_filter=-DeathTest. Runs all non-death tests.
  • ./foo_test –gtest_filter=FooTest.*-FooTest.Bar Runs everything in test case FooTest except FooTest.Bar

Temporarily Disabling Tests

// Tests that Foo does Abc.
TEST(FooTest, DISABLED_DoesAbc) { ... }
class DISABLED_BarTest : public ::testing::Test { ... };
// Tests that Bar does Xyz.
TEST_F(DISABLED_BarTest, DoesXyz) { ... }

Temporarily Enabling Disabled Tests

just invoke the test program with the –gtest_also_run_disabled_tests flag or set the GTEST_ALSO_RUN_DISABLED_TESTS environment variable to a value other than 0.

Repeating the Tests

$ foo_test –gtest_repeat=1000Repeat foo_test 1000 times and don’t stop at failures.
$ foo_test –gtest_repeat=-1A negative count means repeating forever.
$ foo_test –gtest_repeat=1000 –gtest_break_on_failureRepeat foo_test 1000 times, stopping at the first failure. This is especially useful when running under a debugger: when the testfails, it will drop into the debugger and you can then inspect variables and stacks.
$ foo_test –gtest_repeat=1000 –gtest_filter=FooBarRepeat the tests whose name matches the filter 1000 times.
%23%20C/C++%20Test%20Framework%20-%20Google%20Test%20%26%20Google%20Mock%0A@%28myblog%29%5BTDD%2C%20gtest%2C%20gmock%5D%0A%0AGoogle%20Test%uFF0CGoogle%20Mock%u4EE5%u4E0B%u7B80%u79F0gtest%uFF0Cgmock%u3002%0A%u5728%u63A5%u89E6gtest%uFF0Cgmock%u4E4B%u524D%uFF0C%u6D4B%u8BD5C/C++%20code%u4F7F%u7528%5BUnitTest++%5D%28https%3A//github.com/unittest-cpp/unittest-cpp%29%u3002%u8FD9%u662F%u4E00%u4E2A%u5F88%u7B80%u6D01%u7684%u6846%u67B6%uFF0C%u4E0A%u624B%u5F88%u5FEB%u3002%u53C2%u770B%u53E6%u4E00%u7BC7%u535A%u6587%5BUnitTest++%u7B80%u4ECB%5D%28https%3A//zhougy0717.github.io/2016/02/29/UnitTest++%25E7%25AE%2580%25E4%25BB%258B/%29%u3002%u6D4B%u8BD5%u76F8%u5173%u7684%u529F%u80FD%u591F%u7528%uFF0C%u4F46%u662F%u6CA1%u6709mock%u5E93%u3002%u8FD9%u5E26%u6765%u7684%u95EE%u9898%u662F%uFF1A%0A1.%20%u6D4B%u8BD5%u9057%u7559%u4EE3%u7801%u7684%u65F6%u5019%uFF0C%u9700%u8981%u81EA%u884Cfake%u76F8%u5173%u4EE3%u7801%u3002%u8FD9%u4E2A%u5728%u9057%u7559%u7CFB%u7EDF%u5F88%u5E9E%u5927%u65F6%uFF0C%u8981%u9887%u8D39%u5FC3%u529B%u3002%u800C%u4E14%u8FC7%u591A%u6D89%u53CA%u7EC6%u8282%uFF0C%u5BFC%u81F4%u6D4B%u8BD5%u6781%u4E0D%u7A33%u5B9A%uFF0C%u7CFB%u7EDF%u4EE3%u7801%u4EFB%u610F%u7684%u6F14%u8FDB%uFF0C%u90FD%u4F1A%u5BFC%u81F4%u5927%u5806%u7684%u6D4B%u8BD5%u5931%u8D25%uFF0C%u751A%u81F3%u6D4B%u8BD5%u65E0%u6CD5%u8FDB%u884C%u3002%0A2.%20%u6D4B%u8BD5case%u4E4B%u95F4%u65E0%u6CD5%u5F88%u597D%u7684%u89E3%u8026%u3002%u7ED3%u679C%u540C%u6837%u662F%u6D4B%u8BD5%u4E0D%u591F%u7A33%u5B9A%u3002%u672F%u8BED%u662F%u6D4B%u8BD5%u4EE3%u7801%u5F88%u201C%u8106%u5F31%u201D%u3002%0A%0AJava%uFF0CPython%uFF0CJavaScript%u90FD%u6709%u81EA%u5DF1%u7684mock%u5E93%u3002Python%u7684Mock%u7C7B%uFF0CJava%u7684Mockito/PowerMock%uFF0CJavaScript%u7684Sinon%u3002%u4E8E%u662F%uFF0C%u5728%u7F51%u4E0A%u641C%u7D22%u4E86%u4E00%u4E0BC/C++%u7684Mock%u5E93%uFF0C%u4E8E%u662F%u770B%u5230%u4E86gtest%uFF0Cgmock%u3002%u7136%u540E%u5C31%u6709%u4E86%u8FD9%u4E00%u7BC7%u3002%0A%0A%23%23%20%u7B80%u4ECB%0A%u4E0D%u7528%u53BB%u7F51%u4E0A%u8D39%u5FC3%u627E%u6559%u7A0B%uFF0C%u4E24%u4E2A%u9879%u76EE%u7684%u6587%u6863%u90FD%u975E%u5E38%u68D2%u3002%u5165%u53E3%u7EDF%u4E00%u5728%5Bgtest%20GitHub%u9879%u76EE%u4E3B%u9875%5D%28https%3A//github.com/google/googletest%29%u4E0A%u3002%u800C%u4E14%u8BE5%u6587%u6863%u4E0D%u4EC5%u5F88%u597D%u7684%u4ECB%u7ECD%u4E86gtest%uFF0Cgmock%u7684%u7528%u6CD5%uFF0C%u5176%u4E2D%u8FD8%u6D89%u53CA%u4E86%u5F88%u591ATDD%u6216%u8005UnitTest%u7684%u771F%u77E5%u707C%u89C1%uFF0C%u5F88%u503C%u5F97%u8BFB%u4E00%u8BFB%u3002%0A%u8981%u4F7F%u7528gtest%u975E%u5E38%u7B80%u5355%uFF1A%u7F16%u8BD1%u51FAgtest%uFF0Cgmock%uFF0C%u518D%u94FE%u5165%u4F60%u7684%u6D4B%u8BD5%u7A0B%u5E8F%u3002%0A%0A%23%23%23%20%u7F16%u8BD1gtest/gmock%0Agtest%uFF0Cgmock%u5747%u7528cmake%u6765%u7BA1%u7406%u8DE8%u5E73%u53F0%uFF0C%u5148%u7528cmake%u6765%u751F%u6210Makefile%u3002%u7528%u547D%u4EE4%60cmake%20-G%20%22Unix%20Makefiles%22%20/path/to/CMakeList.txt%60%0A%0A%23%23%23%20Makefile%0A%u7136%u540E%u6309%u7167%u4E0B%u9762%u7F16%u5199Makefile%u3002%u6CE8%u610Fgmock_main%u662F%u4E00%u4E2Amain%u51FD%u6570%u6765%u8C03%u7528%u6240%u6709%u7684test%20case%uFF0C%u7701%u5F97%u81EA%u5DF1%u5199main%u51FD%u6570%u4E86%u3002%0A%60%60%60%0ACC%20%3D%20gcc%0ACPP%20%3D%20g++%0ALINK%20%3D%20g++%0ACFLAGS%20%3D%20-g%20-Wall%20-Werror%20-Wextra%20-std%3Dgnu99%0ACPPFLAGS%20%3D%20-g%20-Wall%20-Werror%20-Wextra%0ALIBS%20%3D%20-L./lib%20-lgtest%20-lgmock%20-lgmock_main%20-lpthread%0A%0AC__SOURCES%20%3D%20%24%28wildcard%20*.c%29%0ACPPSOURCES%20%3D%20%24%28wildcard%20*.cpp%29%0AOBJECTS%20%3D%20%24%28patsubst%20%25.c%2C%20%25.o%2C%20%24%28C__SOURCES%29%29%20%24%28patsubst%20%25.cpp%2C%20%25.o%2C%20%24%28CPPSOURCES%29%29%0ATARGET%20%3D%20test_exe%0A%0Afirst%3A%20all%0A%0A%25.o%3A%20%25.c%0A%20%20%20%20%24%28CC%29%20%24%28INCLUDES%29%20-c%20%24%28CFLAGS%29%20-o%20%24@%20%24%3C%0A%0A%25.o%3A%20%25.cpp%0A%20%20%20%20%24%28CPP%29%20%24%28INCLUDES%29%20-c%20%24%28CPPFLAGS%29%20-o%20%24@%20%24%3C%0A%0Aall%3A%20%24%28TARGET%29%0A%0A%24%28TARGET%29%3A%20%24%28OBJECTS%29%0A%20%20%20%20%24%28LINK%29%20%24%28CPPFLAGS%29%20%24%28LIBS%29%20-o%20%24%28TARGET%29%20%24%28OBJECTS%29%0A%0A.PHONY%20%3A%20clean%0A%0Aclean%3A%0A%20%20%20%20rm%20-f%20%24%28TARGET%29%20%24%28OBJECTS%29%0A%60%60%60%0A%0A%23%23%23%20Terms%0A%7C%20Meaning%20%20%20%20%7C%20Google%20Test%20Term%20%7C%20ISTQB%20Term%20%7C%0A%7C%20%3A--------%3A%20%7C%20%3A--------------%3A%20%7C%20%3A--------%3A%20%7C%0A%7C%20Exercise%20a%20particular%20program%20path%20with%20specific%20input%20values%20and%20verify%20the%20results%20%7C%20TEST%28%29%20%7C%20Test%20Case%0A%7CA%20set%20of%20several%20tests%20related%20to%20one%20component%20%7C%20Test%20Case%20%7C%20Test%20Suite%0A%0A%23%23%23%20Test%0A%60%60%60%0A%23include%20%22gtest/gtest.h%22%0A%23include%20%22gmock/gmock.h%22%0A%0Ausing%20%3A%3Atesting%3A%3AReturn%3B%0Ausing%20%3A%3Atesting%3A%3ATest%3B%0Ausing%20%3A%3Atesting%3A%3A_%3B%0Ausing%20%3A%3Atesting%3A%3AAtLeast%3B%0A%0ATEST%28TestCaseName%2C%20should_this_test_do%29%0A%7B%0A%09...%0A%20%20%20%20EXPECT_STREQ%28%22%7B%7D%22%2C%20str%29%3B%0A%7D%0A%60%60%60%0A%u4E2D%u95F4%u7684%u90A3%u5806namespace%u90FD%u662Fgtest/gmock%u5E93%u91CC%u5B9A%u4E49%u7684matcher%u5B8F%u6216%u8005%u5404%u79CD%u6709%u7528%u7684%u5B8F%u3002%0A%0A%23%23%23%20Test%20Fixture%0A%u5728%u6D4B%u8BD5%u6709%u91CD%u590D%u7684%u65F6%u5019%uFF0C%u5C31%u8981%u7528%u5230Test%20Fixture%u4E86%uFF0C%u4E5F%u5C31%u662FsetUp%20/%20tearDown%u3002%0A%60%60%60%0Aclass%20QueueTest%20%3A%20public%20%3A%3Atesting%3A%3ATest%20%7B%0A%20protected%3A%0A%20%20virtual%20void%20SetUp%28%29%20%7B%0A%20%20%20%20q1_.Enqueue%281%29%3B%0A%20%20%20%20q2_.Enqueue%282%29%3B%0A%20%20%20%20q2_.Enqueue%283%29%3B%0A%20%20%7D%0A%0A%20%20//%20virtual%20void%20TearDown%28%29%20%7B%7D%0A%0A%20%20Queue%3Cint%3E%20q0_%3B%0A%20%20Queue%3Cint%3E%20q1_%3B%0A%20%20Queue%3Cint%3E%20q2_%3B%0A%7D%3B%0A%0ATEST_F%28QueueTest%2C%20IsEmptyInitially%29%20%7B%0A%20%20EXPECT_EQ%280%2C%20q0_.size%28%29%29%3B%0A%7D%0A%0ATEST_F%28QueueTest%2C%20DequeueWorks%29%20%7B%0A%20%20int*%20n%20%3D%20q0_.Dequeue%28%29%3B%0A%20%20EXPECT_EQ%28NULL%2C%20n%29%3B%0A%0A%20%20n%20%3D%20q1_.Dequeue%28%29%3B%0A%20%20ASSERT_TRUE%28n%20%21%3D%20NULL%29%3B%0A%20%20EXPECT_EQ%281%2C%20*n%29%3B%0A%20%20EXPECT_EQ%280%2C%20q1_.size%28%29%29%3B%0A%20%20delete%20n%3B%0A%0A%20%20n%20%3D%20q2_.Dequeue%28%29%3B%0A%20%20ASSERT_TRUE%28n%20%21%3D%20NULL%29%3B%0A%20%20EXPECT_EQ%282%2C%20*n%29%3B%0A%20%20EXPECT_EQ%281%2C%20q2_.size%28%29%29%3B%0A%20%20delete%20n%3B%0A%7D%0A%60%60%60%0A%23%23%23%23%20constructor/destructor%20vs.%20SetUp/TearDown%0A%3EWhen%20you%20need%20to%20write%20per-test%20set-up%20and%20tear-down%20logic%2C%20you%20have%20the%20choice%20between%20using%20the%20test%20fixture%20constructor/destructor%20or%20SetUp%28%29/TearDown%28%29.%20The%20former%20is%20usually%20preferred%2C%20as%20it%20has%20the%20following%20benefits%3A%0A%0A%3E-%20By%20initializing%20a%20member%20variable%20in%20the%20constructor%2C%20we%20have%20the%20option%20to%20make%20it%20const%2C%20which%20helps%20prevent%20accidental%20changes%20to%20its%20value%20and%20makes%20the%20tests%20more%20obviously%20correct.%0A-%20**In%20case%20we%20need%20to%20subclass%20the%20test%20fixture%20class%2C%20the%20subclass%27%20constructor%20is%20guaranteed%20to%20call%20the%20base%20class%27%20constructor%20first%2C%20and%20the%20subclass%27%20destructor%20is%20guaranteed%20to%20call%20the%20base%20class%27%20destructor%20afterward.%20**With%20SetUp%28%29/TearDown%28%29%2C%20a%20subclass%20may%20make%20the%20mistake%20of%20forgetting%20to%20call%20the%20base%20class%27%20SetUp%28%29/TearDown%28%29%20or%20call%20them%20at%20the%20wrong%20moment.%0A%0A%3E%20Benefit%20for%20using%20SetUp/TearDown%3A%0A%0A%3E-%20If%20the%20tear-down%20operation%20could%20throw%20an%20exception%2C%20you%20must%20use%20TearDown%28%29%20as%20opposed%20to%20the%20destructor%2C%20as%20throwing%20in%20a%20destructor%20leads%20to%20undefined%20behavior%20and%20usually%20will%20kill%20your%20program%20right%20away.%20Note%20that%20many%20standard%20libraries%20%28like%20STL%29%20may%20throw%20when%20exceptions%20are%20enabled%20in%20the%20compiler.%20Therefore%20you%20should%20prefer%20TearDown%28%29%20if%20you%20want%20to%20write%20portable%20tests%20that%20work%20with%20or%20without%20exceptions.%0A-%20The%20assertion%20macros%20throw%20an%20exception%20when%20flag%20--gtest_throw_on_failure%20is%20specified.%20Therefore%2C%20you%20shouldn%27t%20use%20Google%20Test%20assertions%20in%20a%20destructor%20if%20you%20plan%20to%20run%20your%20tests%20with%20this%20flag.%0A-%20In%20a%20constructor%20or%20destructor%2C%20you%20cannot%20make%20a%20virtual%20function%20call%20on%20this%20object.%20%28You%20can%20call%20a%20method%20declared%20as%20virtual%2C%20but%20it%20will%20be%20statically%20bound.%29%20Therefore%2C%20if%20you%20need%20to%20call%20a%20method%20that%20will%20be%20overriden%20in%20a%20derived%20class%2C%20you%20have%20to%20use%20SetUp%28%29/TearDown%28%29.%0A%0A%u7B80%u8A00%u4E4B%uFF0C%u5728%u903B%u8F91%u4E0A%uFF0C%u8FD9%u4E24%u7EC4%u7684%u4F5C%u7528%u76F8%u540C%uFF0C%u90FD%u662F%u6BCF%u4E2A%u6D4B%u8BD5%u4E4B%u524D%u4E4B%u540E%u4F1A%u505A%u4E00%u4E9B%u5904%u7406%u5DE5%u4F5C%u3002Constructor/Destructor%u7684%u597D%u5904%u662F%u63D0%u4F9B%u4E86%u7EE7%u627F%u3002setUp/tearDown%u7684%u597D%u5904%u662F%u53EF%u4EE5%u5904%u7406exception%uFF0C%u8FD9%u662F%u4E0D%u80FD%u653E%u5728%u6790%u6784%u51FD%u6570%u91CC%u7684%u3002%0A%23%23%23%20SetUpTestCase%20/%20TearDownTestCase%0ATest%20Case%u7EA7%u522B%u7684SetUp/TearDown%0A%60%60%60%0Aclass%20FooTest%20%3A%20public%20%3A%3Atesting%3A%3ATest%20%7B%0A%20protected%3A%0A%20%20//%20Per-test-case%20set-up.%0A%20%20//%20Called%20before%20the%20first%20test%20in%20this%20test%20case.%0A%20%20//%20Can%20be%20omitted%20if%20not%20needed.%0A%20%20static%20void%20SetUpTestCase%28%29%20%7B%0A%20%20%20%20shared_resource_%20%3D%20new%20...%3B%0A%20%20%7D%0A%0A%20%20//%20Per-test-case%20tear-down.%0A%20%20//%20Called%20after%20the%20last%20test%20in%20this%20test%20case.%0A%20%20//%20Can%20be%20omitted%20if%20not%20needed.%0A%20%20static%20void%20TearDownTestCase%28%29%20%7B%0A%20%20%20%20delete%20shared_resource_%3B%0A%20%20%20%20shared_resource_%20%3D%20NULL%3B%0A%20%20%7D%0A%0A%20%20//%20You%20can%20define%20per-test%20set-up%20and%20tear-down%20logic%20as%20usual.%0A%20%20virtual%20void%20SetUp%28%29%20%7B%20...%20%7D%0A%20%20virtual%20void%20TearDown%28%29%20%7B%20...%20%7D%0A%0A%20%20//%20Some%20expensive%20resource%20shared%20by%20all%20tests.%0A%20%20static%20T*%20shared_resource_%3B%0A%7D%3B%0A%60%60%60%0A%23%23%23%20SetUp/TearDown%20Environment%20%0A%3E-%20First%2C%20you%20subclass%20the%20%3A%3Atesting%3A%3AEnvironment%20class%20to%20define%20a%20test%20environment%2C%20which%20knows%20how%20to%20set-up%20and%20tear-down%3A%0A%3E-%20Then%2C%20you%20register%20an%20instance%20of%20your%20environment%20class%20with%20Google%20Test%20by%20calling%20the%20%60%3A%3Atesting%3A%3AAddGlobalTestEnvironment%28%29%60%20function%3A%0A%3ENow%2C%20when%20RUN_ALL_TESTS%28%29%20is%20called%2C%20it%20first%20calls%20the%20SetUp%28%29%20method%20of%20the%20environment%20object%2C%20then%20runs%20the%20tests%20if%20there%20was%20no%20fatal%20failures%2C%20and%20finally%20calls%20TearDown%28%29%20of%20the%20environment%20object.%0A-%20It%27s%20OK%20to%20register%20multiple%20environment%20objects.%20In%20this%20case%2C%20their%20SetUp%28%29%20will%20be%20called%20in%20the%20order%20they%20are%20registered%2C%20and%20their%20TearDown%28%29%20will%20be%20called%20in%20the%20reverse%20order.%0A-%20Note%20that%20Google%20Test%20takes%20ownership%20of%20the%20registered%20environment%20objects.%20Therefore%20do%20not%20delete%20them%20by%20yourself.%0A%0A%60%60%60%0Aclass%20Environment%20%7B%0A%20public%3A%0A%20%20virtual%20%7EEnvironment%28%29%20%7B%7D%0A%20%20//%20Override%20this%20to%20define%20how%20to%20set%20up%20the%20environment.%0A%20%20virtual%20void%20SetUp%28%29%20%7B%7D%0A%20%20//%20Override%20this%20to%20define%20how%20to%20tear%20down%20the%20environment.%0A%20%20virtual%20void%20TearDown%28%29%20%7B%7D%0A%7D%3B%0A%0AEnvironment*%20AddGlobalTestEnvironment%28Environment*%20env%29%3B%0A%60%60%60%0A%23%23%23%20%u65AD%u8A00%0A%u6709%u4E24%u79CD%u65AD%u8A00EXPECT%5C_xxx%u548CASSERT%5C_xxx%u3002%u524D%u8005%u4F1A%u8BA9%u6D4B%u8BD5%u7EC8%u6B62%uFF0C%u540E%u8005%u4E0D%u4F1A%uFF0C%u53EA%u4F1A%u8BA9%u6D4B%u8BD5fail%u3002%0A%0A%0A%23%23%20gmock%0A%u4E4B%u6240%u4EE5%u8981%u5207%u5230gtest%uFF0C%u552F%u4E00%u7684%u539F%u56E0%u5C31%u662Fgmock%uFF0C%u6240%u4EE5%u8981%u4E13%u5F00%u4E00%u7AE0%u91CD%u70B9%u4ECB%u7ECD%u4E00%u4E0B%u3002%u6240%u6709%u5185%u5BB9%u5747%u6765%u81EA%u4E8E%u5B98%u65B9%u6587%u6863%u3002%u5185%u5BB9%u6DF1%u5EA6%u7531%u6D45%u5165%u6DF1%uFF0C%u4F9D%u6B21%u5982%u4E0B%uFF1A%0A-%20%5BGoogle%20Mock%20for%20Dummies%5D%28https%3A//github.com/google/googletest/blob/master/googlemock/docs/ForDummies.md%29%0A-%20%5BCookBook%5D%28https%3A//github.com/google/googletest/blob/master/googlemock/docs/CookBook.md%29%0A%0A%u6700%u540E%u8FD8%u6709%u53C2%u8003%u624B%u518C%3A%0A-%20%5BCheat%20Sheet%5D%28https%3A//github.com/google/googletest/blob/master/googlemock/docs/CheatSheet.md%29%0A%0A%23%23%23%20%u7B80%u4ECB%0A%3EGoogle%20C++%20Mocking%20Framework%20%28or%20Google%20Mock%20for%20short%29%20is%20a%20library%20%28sometimes%20we%20also%20call%20it%20a%20%22framework%22%20to%20make%20it%20sound%20cool%29%20for%20creating%20mock%20classes%20and%20using%20them.%20It%20does%20to%20C++%20what%20jMock%20and%20EasyMock%20do%20to%20Java.%0A%0A%23%23%23%23%20%u4F55%u4E3AMock%uFF1F%0A%3E**Mocks**%20are%20objects%20pre-programmed%20with%20expectations%2C%20which%20form%20a%20specification%20of%20the%20calls%20they%20are%20expected%20to%20receive.%0A%0A%u76F8%u5E94%u7684%u8FD8%u6709Fake%u548CStub%0A%3E**Fake**%20objects%20have%20working%20implementations%2C%20but%20usually%20take%20some%20shortcut%20%28perhaps%20to%20make%20the%20operations%20less%20expensive%29%2C%20which%20makes%20them%20not%20suitable%20for%20production.%20An%20in-memory%20file%20system%20would%20be%20an%20example%20of%20a%20fake.%0A%0Agmock%u7684%u6587%u6863%u91CC%u53EA%u63D0%u5230%u4E86Fake%uFF0C%u4ECEMartin%20Fowler%u7684%u6587%u7AE0%5BMocks%20Aren%27t%20Stubs%5D%28https%3A//martinfowler.com/articles/mocksArentStubs.html%29%u4E2D%u6458%u5F55%u5982%u4E0B%3A%0A%3E**Stubs**%20provide%20canned%20answers%20to%20calls%20made%20during%20the%20test%2C%20usually%20not%20responding%20at%20all%20to%20anything%20outside%20what%27s%20programmed%20in%20for%20the%20test.%0A%0A%u4E09%u8005%u4F5C%u7528%u76F8%u540C%uFF0C%u90FD%u662F%u6A21%u62DF%u7CFB%u7EDF%u5176%u4ED6%u90E8%u5206%u7684%u529F%u80FD%uFF0C%u8FBE%u5230%u4EE3%u7801%u9694%u79BB%u7684%u6548%u679C%uFF0C%u65B9%u4FBF%u6D4B%u8BD5%u3002%u4F46%u662FMock%u7684%u7279%u70B9%u662F%u66F4OO%u5316%uFF0C%u4E5F%u7B26%u5408TDD%u6216%u8005BDD%u7684%u601D%u60F3%u2014%u2014%u9488%u5BF9%u4E00%u4E2Aobject%u8BBE%u7F6E%u671F%u5F85%uFF0C%u518D%u5BF9%u9F50verify%u3002%0A%0AMocks%20vs.%20Stubs%20-%20from%20Martin%20Fowler%0A%3EIn%20order%20to%20use%20state%20verification%20on%20the%20stub%2C%20I%20need%20to%20make%20some%20extra%20methods%20on%20the%20stub%20to%20help%20with%20verification.%20As%20a%20result%20the%20stub%20implements%20MailService%20but%20adds%20extra%20test%20methods.%0A%0A%3EMock%20objects%20always%20use%20behavior%20verification%2C%20a%20stub%20can%20go%20either%20way.%20Meszaros%20refers%20to%20stubs%20that%20use%20behavior%20verification%20as%20a%20Test%20Spy.%20The%20difference%20is%20in%20how%20exactly%20the%20double%20runs%20and%20verifies%20and%20I%27ll%20leave%20that%20for%20you%20to%20explore%20on%20your%20own.%0A%0A%23%23%23%23%20Getting%20Started%0AClass%20to%20Mock%0A%60%60%60%0Aclass%20Turtle%20%7B%0A%20%20...%0A%20%20virtual%20%7ETurtle%28%29%20%7B%7D%0A%20%20virtual%20void%20PenUp%28%29%20%3D%200%3B%0A%20%20virtual%20void%20PenDown%28%29%20%3D%200%3B%0A%20%20virtual%20void%20Forward%28int%20distance%29%20%3D%200%3B%0A%7D%3B%0A%60%60%60%0AMock%20class%0A%60%60%60%0A%23include%20%22gmock/gmock.h%22%20%20//%20Brings%20in%20Google%20Mock.%0Aclass%20MockTurtle%20%3A%20public%20Turtle%20%7B%0A%20public%3A%0A%20%20...%0A%20%20MOCK_METHOD0%28PenUp%2C%20void%28%29%29%3B%0A%20%20MOCK_METHOD0%28PenDown%2C%20void%28%29%29%3B%0A%20%20MOCK_METHOD1%28Forward%2C%20void%28int%20distance%29%29%3B%0A%7D%3B%0A%60%60%60%0A%u9488%u5BF9%u5F85Mock%u7684Turtle%20class%u8981%u6CE8%u610F%u7684%u662F%3A%0A%3ENote%20that%20the%20**destructor%20of%20Turtle%20must%20be%20virtual**%2C%20as%20is%20the%20case%20for%20all%20classes%20you%20intend%20to%20inherit%20from%20-%20otherwise%20the%20destructor%20of%20the%20derived%20class%20will%20not%20be%20called%20when%20you%20delete%20an%20object%20through%20a%20base%20pointer%2C%20and%20you%27ll%20get%20corrupted%20program%20states%20like%20memory%20leaks.%0A%0AUse%20it%0A%60%60%60%0A%23include%20%22path/to/mock-turtle.h%22%0A%23include%20%22gmock/gmock.h%22%0A%23include%20%22gtest/gtest.h%22%0Ausing%20%3A%3Atesting%3A%3AAtLeast%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20//%20%231%0A%0ATEST%28PainterTest%2C%20CanDrawSomething%29%20%7B%0A%20%20MockTurtle%20turtle%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20//%20%232%0A%20%20EXPECT_CALL%28turtle%2C%20PenDown%28%29%29%20%20%20%20%20%20%20%20%20%20%20%20%20%20//%20%233%0A%20%20%20%20%20%20.Times%28AtLeast%281%29%29%3B%0A%0A%20%20Painter%20painter%28%26turtle%29%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20//%20%234%0A%0A%20%20EXPECT_TRUE%28painter.DrawCircle%280%2C%200%2C%2010%29%29%3B%0A%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20//%20%235%0A%0Aint%20main%28int%20argc%2C%20char**%20argv%29%20%7B%0A%20%20//%20The%20following%20line%20must%20be%20executed%20to%20initialize%20Google%20Mock%0A%20%20//%20%28and%20Google%20Test%29%20before%20running%20the%20tests.%0A%20%20%3A%3Atesting%3A%3AInitGoogleMock%28%26argc%2C%20argv%29%3B%0A%20%20return%20RUN_ALL_TESTS%28%29%3B%0A%7D%0A%60%60%60%0A%u5982%u679C%u4F60%u613F%u610F%u7684%u8BDD%uFF0C%u53EF%u4EE5%u81EA%u5DF1%u5199main%uFF0C%u5982%u679C%u4F60%u60F3%u5077%u61D2%uFF0C%u8BB0%u5F97%u5728Makefile%u91CClink%20gmock_main%u3002%0A%u6309Google%u7684%u8BF4%u6CD5%uFF0Cgmock%u4E5F%u53EF%u4EE5%u548C%u5176%u4ED6%u6D4B%u8BD5%u6846%u67B6%u517C%u5BB9%uFF0C%u4F46%u603B%u611F%u89C9%u633A%u60AC%u7684%u3002%0A%0A%23%23%23%23%20%u4E00%u4E9B%u6709%u7528%u7684%u5DE5%u5177%0A**Marcher**%0A%60EXPECT_CALL%28turtle%2C%20Forward%28_%29%29%3B%60%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%u4EE5%u4EFB%u610F%u53C2%u6570%u8C03%u7528Forward%0A%60EXPECT_CALL%28turtle%2C%20Forward%28Ge%28100%29%29%29%3B%60%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%u4EE5%u5927%u4E8E100%u7684%u6574%u6570%u8C03%u7528Forward%0A%09%0A**Cardinalities%3A%20How%20Many%20Times%20Will%20It%20Be%20Called%3F**%0A%3E-%20If%20neither%20WillOnce%28%29%20nor%20WillRepeatedly%28%29%20is%20in%20the%20EXPECT_CALL%28%29%2C%20the%20inferred%20cardinality%20is%20Times%281%29.%0A-%20If%20there%20are%20n%20WillOnce%28%29%27s%20but%20no%20WillRepeatedly%28%29%2C%20where%20n%20%3E%3D%201%2C%20the%20cardinality%20is%20Times%28n%29.%0A-%20If%20there%20are%20n%20WillOnce%28%29%27s%20and%20one%20WillRepeatedly%28%29%2C%20where%20n%20%3E%3D%200%2C%20the%20cardinality%20is%20Times%28AtLeast%28n%29%29.%0A%0A%60%60%60%0AEXPECT_CALL%28mockObj%2C%20func%28%29%29%0A%09.Times%28AtLeast%281%29%29%0A%09.WillOnce%28Return%28123%29%29%0A%09.WillRepeatedly%28Return%28456%29%29%0A%60%60%60%0A%u4E0A%u9762%u8FD9%u6BB5%u4EE3%u7801%u8981%u6C42func%u51FD%u6570%u81F3%u5C11%u8FD0%u884C%u4E00%u6B21%uFF0C%u7B2C%u4E00%u6B21%u8FD4%u56DE123%uFF0C%u4E4B%u540E%u6BCF%u6B21%u8FD4%u56DE456%u3002%0A%0A%3E**Important%20note**%3A%20The%20EXPECT_CALL%28%29%20statement%20evaluates%20the%20action%20clause%20only%20once%2C%20even%20though%20the%20action%20may%20be%20performed%20many%20times.%20Therefore%20you%20must%20be%20careful%20about%20side%20effects.%20The%20following%20may%20not%20do%20what%20you%20want%3A%0A%0A%60%60%60%0Aint%20n%20%3D%20100%3B%0AEXPECT_CALL%28turtle%2C%20GetX%28%29%29%0A.Times%284%29%0A.WillRepeatedly%28Return%28n++%29%29%3B%0A%60%60%60%0A%u56E0%u4E3AReturn%u662F%u5B8F%uFF0C%u6240%u4EE5%u53EA%u4F1A%u66FF%u6362%u4E00%u6B21%uFF0C%u6240%u4EE5%u4E0D%u7BA1GetX%u8C03%u7528%u51E0%u6B21%uFF0C%u8FD4%u56DE%u90FD%u662F101%uFF0C%u800C%u4E0D%u662F101%2C102%2C103%2C...%0A%0A**All%20Expectations%20Are%20Sticky**%0A%u6240%u8C13%u7684sticky%u5C31%u662FEXPECT%5C_CALL%u603B%u662F%u751F%u6548%u7684%uFF0C%u9664%u975E%u4F60%u663E%u793A%u7684%u5C06%u5176%u5931%u6548%u3002%u5982%u4E0B%uFF0C%u6240%u6709%u7684EXPECT%5C_CALL%u90FD%u4F1A%u751F%u6548%uFF0C%u90A3%u4E48%u6700%u540E%u4E00%u4E2A%u4F1A%u8986%u76D6%u524D%u9762%u6240%u6709%u7684%uFF0C%u4E5F%u5C31%u662FGetX%u603B%u4F1A%u8FD4%u56DE10%u3002%0A%60%60%60%0Ausing%20%3A%3Atesting%3A%3AReturn%3B%0A...%0Afor%20%28int%20i%20%3D%20n%3B%20i%20%3E%200%3B%20i--%29%20%7B%0A%20%20EXPECT_CALL%28turtle%2C%20GetX%28%29%29%0A%20%20%20%20%20%20.WillOnce%28Return%2810*i%29%29%3B%0A%7D%0A%60%60%60%0A%u5982%u679C%u5E0C%u671B%u4ED6%u8FD4%u56DE%uFF0C30%2C20%2C10%uFF0C...%uFF0C%u5E94%u8BE5%u8FD9%u4E48%u5199%0A%60%60%60%0Ausing%20%3A%3Atesting%3A%3AReturn%3B%0A...%0Afor%20%28int%20i%20%3D%20n%3B%20i%20%3E%200%3B%20i--%29%20%7B%0A%20%20EXPECT_CALL%28turtle%2C%20GetX%28%29%29%0A%20%20%20%20.WillOnce%28Return%2810*i%29%29%0A%20%20%20%20.RetiresOnSaturation%28%29%3B%0A%7D%0A%60%60%60%0A%60RetireOnSaturation%60%u5C31%u662F%u663E%u793A%u7684%u8BA9%u5176%u5931%u6548%u3002%u8FD8%u6709%u4E00%u4E2A%u529E%u6CD5%uFF1A%0A%60%60%60%0Ausing%20%3A%3Atesting%3A%3AInSequence%3B%0Ausing%20%3A%3Atesting%3A%3AReturn%3B%0A...%0A%7B%0A%20%20InSequence%20s%3B%0A%0A%20%20for%20%28int%20i%20%3D%201%3B%20i%20%3C%3D%20n%3B%20i++%29%20%7B%0A%20%20%20%20EXPECT_CALL%28turtle%2C%20GetX%28%29%29%0A%20%20%20%20%20%20%20%20.WillOnce%28Return%2810*i%29%29%0A%20%20%20%20%20%20%20%20.RetiresOnSaturation%28%29%3B%0A%20%20%7D%0A%7D%0A%60%60%60%0A%u56E0%u4E3AInSequence%u7684%u5173%u7CFB%uFF0C%u5728%u524D%u9762%u7684EXPECT%5C_CALL%u751F%u6548%u4EE5%u540E%uFF0C%u5C31%u4F1A%u81EA%u52A8%u5931%u6548%uFF0C%u6240%u4EE5%u4E0D%u4F1A%u4EA7%u751F%u8986%u76D6%u7684%u6548%u679C%u3002%0A**Ordered%20vs%20Unordered%20Calls**%0A%60%60%60%0Ausing%20%3A%3Atesting%3A%3AInSequence%3B...%0ATEST%28FooTest%2C%20DrawsLineSegment%29%20%7B%0A%20%20...%0A%20%20%7B%0A%20%20%20%20InSequence%20dummy%3B%0A%0A%20%20%20%20EXPECT_CALL%28turtle%2C%20PenDown%28%29%29%3B%0A%20%20%20%20EXPECT_CALL%28turtle%2C%20Forward%28100%29%29%3B%0A%20%20%20%20EXPECT_CALL%28turtle%2C%20PenUp%28%29%29%3B%0A%20%20%7D%0A%20%20Foo%28%29%3B%0A%7D%0A%60%60%60%0A**Expecting%20Partially%20Ordered%20Calls**%0AGoogle%20Mock%20allows%20you%20to%20impose%20an%20arbitrary%20DAG%20%28directed%20acyclic%20graph%29%20on%20the%20calls.%20One%20way%20to%20express%20the%20DAG%20is%20to%0Ause%20the%20After%20clause%20of%20EXPECT_CALL%20.%0A%60%60%60%0Ausing%20%3A%3Atesting%3A%3ASequence%3B%0ASequence%20s1%2C%20s2%3B%0AEXPECT_CALL%28foo%2C%20A%28%29%29%0A%09.InSequence%28s1%2C%20s2%29%3B%0AEXPECT_CALL%28bar%2C%20B%28%29%29%0A%09.InSequence%28s1%29%3B%0AEXPECT_CALL%28bar%2C%20C%28%29%29%0A%09.InSequence%28s2%29%3B%0AEXPECT_CALL%28foo%2C%20D%28%29%29%0A%09.InSequence%28s2%29%3B%0A%60%60%60%0Aspecifies%20the%20following%20DAG%20%28where%20s1%20is%20A%20-%3E%20B%20%2C%20and%20s2%20is%20A%20-%3E%20C%20-%3E%20D%20%29%3A%0A%60%60%60%0A%09%20+---%3E%20B%0A%20%20%20%20%20%7C%0AA%20---%7C%0A%09%20%7C%0A%09%20+---%3E%20C%20---%3E%20D%0A%60%60%60%0A**Uninteresting%20Calls**%0A%u8FD9%u662Fgmock%u62A5%u7684warning%u3002%u5F53%u9488%u5BF9%u67D0%u4E00%u4E2A%u5F85%u6D4B%u51FD%u6570%u8BBE%u7F6E%u4E86EXPECT%20spec%uFF0C%u5374%u5E76%u6CA1%u6709%u8C03%u7528%u7684%u65F6%u5019%uFF0C%u5C31%u4F1A%u62A5%u8FD9%u4E2Awarning%u3002%u6B64%u65F6gmock%u8BA4%u4E3A%u4F60%u5BF9%u8BE5%u51FD%u6570%u5E76%u6CA1%u6709%u5174%u8DA3%uFF0C%u6240%u4EE5%u5C31%u4E0D%u9700%u8981%u8FD9%u4E2AEXPECT%20spec%u3002%u5F53%u7136%u4F60%u53EF%u4EE5%u9009%u62E9%u5FFD%u7565%u8FD9%u4E2Awarning%uFF0C%u4F46%u6211%u8BA4%u4E3A%u51FA%u8FD9%u4E2Awarning%u7684%u65F6%u5019%uFF0C%u591A%u534A%u662Ftest%u6F0F%u5199%u4E86%u4EC0%u4E48%u3002%0A%0A**Returning%20Live%20Values%20from%20Mock%20Methods**%0A%60%60%60%0Ausing%20testing%3A%3AByRef%3B%0Ausing%20testing%3A%3AReturn%3B%0Aclass%20MockFoo%20%3A%20public%20Foo%20%7B%0A%09public%3A%0A%09%09MOCK_METHOD0%28GetValue%2C%20int%28%29%29%3B%0A%7D%3B%0A...%0Aint%20x%20%3D%200%3B%0AMockFoo%20foo%3B%0AEXPECT_CALL%28foo%2C%20GetValue%28%29%29%0A%09//%20.WillRepeatedly%28Return%28ByRef%28x%29%29%29%3B%20X%20error%0A%09.WillRepeatedly%28ReturnPointee%28x%29%29%3B%0Ax%20%3D%2042%3B%0AEXPECT_EQ%2842%2C%20foo.GetValue%28%29%29%3B%0A%60%60%60%0A%0A%23%23%23%20%u4E00%u4E9B%u6D4B%u8BD5case%0A%u5199Unit%20Test%u5E76%u4E0D%u50CF%u60F3%u50CF%u7684%u90A3%u4E48%u7B80%u5355%uFF0C%u5E76%u4E0D%u662F%u8C03%u7528%u4E86%u6846%u67B6%uFF0C%u9488%u5BF9%u6BCF%u4E2A%u51FD%u6570%u5199test%20case%u5C31%u53EF%u4EE5%u3002%u6309%u6211%u76EE%u524D%u7684%u7406%u89E3%u6709%u4EE5%u4E0B%u51E0%u79CD%u6311%u6218%uFF1A%0A1.%20Test%20Case%u5982%u4F55%u89E3%u8026%u3002%u4E0D%u8981%u6709%u91CD%u590D%u6D4B%u8BD5%28overlap%29%u3002%0A%u4F8B%u5982%uFF1A%u5728%u5199A%u51FD%u6570%u7684%u65F6%u5019%uFF0C%u5199%u4E86%u6D4B%u8BD5testA%uFF0CB%u51FD%u6570%u4F1A%u8C03%u7528A%u51FD%u6570%uFF0C%u90A3%u4E48%u5728%u5199%u5B8CA%u4E4B%u540E%u5199B%u7684%u6D4B%u8BD5testB%u65F6%uFF0C%u662F%u5426%u8981%u5C06A%20mock/fake/stub%u6389%3F%20%u5982%u679C%u4E0D%u5C06A%u51FD%u6570Fake%u6389%uFF0C%u5219testA%u548CtestB%u4E4B%u95F4%u5C31%u662F%u6709overlap%u3002%u6211%u8BA4%u4E3A%u8FD9%u4E4B%u95F4%u53EF%u4EE5%u6709%u53D6%u820D%uFF0C%u6700%u4F73%u72B6%u6001%u5E94%u5F53%u662F%u6B64%u65F6%uFF0C%u5C06testA%u5220%u9664%uFF0C%u53EA%u4FDD%u7559testB%u3002%u4F46%u4ECD%u5E94%u6839%u636E%u5177%u4F53%u60C5%u51B5%u800C%u5B9A%u3002%0A%0A2.%20%u5982%u4F55%u9488%u5BF9%u4F9D%u8D56%u5173%u7CFB%u8FDB%u884Cmock%u5316%u3002%0A%u4F8B%u5982%u51FA%u73B0%u8FD9%u6837%u7684%u8BED%u53E5%3A%20%60B%20%3D%20new%20A%60%uFF0C%u5219%u7C7BB%u4F9D%u8D56%u4E8E%u7C7BA%u3002%u4F46%u7C7BA%u5E76%u6CA1%u6709%u5FC5%u8981%u7F16%u8BD1%u8FDBtest%u3002%u56E0%u4E3A%u4E00%u65E6%u52A0%u5165A%uFF0C%u5219%u52BF%u5FC5%u4F1A%u5F15%u5165%u66F4%u591A%u4F9D%u8D56%u5173%u7CFB%uFF0C%u800C%u5BFC%u81F4test%u7F16%u8BD1%u5D29%u6E83%u3002%u4F9D%u8D56%u5173%u7CFB%u7684%u89E3%u51B3%u65E0%u7A77%u65E0%u5C3D%u3002%u5728%u505AAndroid%u7684%u5355%u5143%u6D4B%u8BD5%u65F6%uFF0C%u53EF%u4EE5%u7528PowerMock%u53D6%u4EE3Mockito%u6765Mock%u6784%u9020%u51FD%u6570%uFF0C%u5C06%u6784%u9020%u51FD%u6570Fake%u5316%u6210%u7C7B%u4F3C%u5DE5%u5382%u51FD%u6570%uFF0C%u8FD4%u56DE%u7C7B%u5B9E%u4F8B%u3002%u5177%u4F53%u53C2%u770B%u8FD9%u7BC7%u535A%u6587%5BAndroid%u5355%u5143%u6D4B%u8BD5%5D%28https%3A//zhougy0717.github.io/2016/10/19/Android%25E5%258D%2595%25E5%2585%2583%25E6%25B5%258B%25E8%25AF%2595/%29%u3002%0A%u4F46%u5B9E%u9645%u4E0A%uFF0C%u6309%u7167%u73B0%u5728%u7684%u7406%u89E3%uFF0C%u5176%u5B9EMock%u6784%u9020%u51FD%u6570%u662F%u4E0D%u53EF%u53D6%u7684%uFF0C%u9996%u5148%u9020%u6210%u88AB%u6D4B%u4EE3%u7801spec%u4E0D%u6E05%u6670%uFF0C%u8BD5%u60F3%u4E00%u4E2A%u6784%u9020%u51FD%u6570%u600E%u4E48%u4F1A%u8FD4%u56DE%u53E6%u4E00%u4E2A%u7C7B%u7684%u5B9E%u4F8B%u3002%u5176%u6B21%uFF0C%u5728C++%u4E2D%u5F88%u96BE%u505A%u5230Mock%u6784%u9020%u51FD%u6570%u3002%u597D%u7684%u505A%u6CD5%u5E94%u5F53%u662F%u8FD0%u7528Dependency%20Injection%u3002%u4F8B%u5982%uFF1A%0A%60%60%60%0Aclass%20A%20%0A%7B%7D%0A%0Aclass%20B%0A%7B%0A%09void%20func%28%29%0A%09%7B%0A%09%09A*%20a%20%3D%20new%20A%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%u7C7BB%u5E94%u5F53%u6539%u5199%u4E3A%uFF1A%0A%60%60%60%0Aclass%20B%0A%7B%0A%09void%20func%28A*%29%0A%09%7B%0A%09%09...%0A%09%7D%0A%7D%0A%60%60%60%0A%u5C06%u7C7BA%u6307%u9488%u4F20%u5165%uFF0C%u89E3%u51B3dependency%u7684%u95EE%u9898%u3002%0A%0A3.%20%u5982%u4F55%u80FD%u591F%u8BA9%u6D4B%u8BD5%u7A33%u5B9A%uFF0C%u5728%u4EFB%u610F%u73AF%u5883%u4E0B%u5747%u8FD4%u56DE%u540C%u6837%u7684%u6D4B%u8BD5%u7ED3%u679C%u3002%0A%u8FD9%u4E2A%u4E00%u822C%u6D89%u53CA%u6D4B%u8BD5%u73AF%u5883%u7684%u5F71%u54CD%u3002%u4F8B%u5982%u8C03%u7528%u7F51%u7EDC%u76F8%u5173%u7684%u529F%u80FD%uFF0C%u5728%u6CA1%u6709%u7F51%u7EDC%u7684%u73AF%u5883%u5C31%u6CA1%u6CD5%u8FDB%u884C%u3002%u518D%u4F8B%u5982%u6D4B%u8BD5%u65F6%u64CD%u4F5C%u771F%u5B9E%u5177%u4F53%u6587%u4EF6%uFF0C%u5219%u8BE5%u6587%u4EF6%u88AB%u6D4B%u8BD5%u5916%u4EBA%u4E3A%u6216%u4EE3%u7801%u4FEE%u6539%uFF0C%u5219%u6D4B%u8BD5%u53EF%u80FD%u5C31%u4F1A%u83AB%u540D%u5931%u8D25%u3002%0A%u9488%u5BF9%u8FD9%u4E9B%u60C5%u51B5%uFF0C%u6211%u4EEC%u5E94%u5F53%u5728%u6D4B%u8BD5%u4E2D%u5C3D%u91CF%u907F%u514D%u3002%u4F8B%u5982%u524D%u8005%uFF0C%u6211%u4EEC%u5E94%u5F53%u5BF9%u7F51%u7EDC%u63A5%u53E3%u8FDB%u884CMock%u5316%uFF0C%u540E%u8005%u5E94%u5F53%u5728%u6D4B%u8BD5%u7684setUp%u548CtearDown%u4E2D%u751F%u6210%u865A%u5047%u6587%u4EF6%u7528%u4E8E%u6D4B%u8BD5%uFF0C%u5E76%u5728%u6D4B%u8BD5%u5B8C%u6210%u65F6%u505A%u6E05%u7406%u3002%0A%0A4.%20%u4F1A%u56E0%u4E3A%u5F88%u5C0F%u7684%u88AB%u6D4B%u4EE3%u7801%u6539%u52A8%uFF0C%u800C%u5BFC%u81F4%u5927%u9762%u79EF%u6D4B%u8BD5%u5931%u8D25%uFF0C%u751A%u81F3%u6D4B%u8BD5%u5D29%u6E83%u3002%0A%u8FD9%u4E2A%u5C31%u662Fgmock%u6587%u6863%u4E2D%u63D0%u5230%u7684%u8981%u9488%u5BF9%u63A5%u53E3%u7F16%u7A0B%uFF0C%u9488%u5BF9%u63A5%u53E3%u6D4B%u8BD5%u3002Robert%20C%B7Martin%u5728%u300A%u654F%u6377%u8F6F%u4EF6%u5F00%u53D1%uFF0D%u539F%u5219%u3001%u6A21%u5F0F%u4E0E%u5B9E%u8DF5%u300B%u4E00%u4E66%u4E2D%u6709%u63D0%u51FA%uFF1A%u6240%u6709%u7684%u4EE3%u7801%u90FD%u5E94%u4F9D%u8D56%u4E8E%u62BD%u8C61%u63A5%u53E3%u3002%u56E0%u4E3A%u62BD%u8C61%u63A5%u53E3%u662F%u7ECF%u8FC7%u62BD%u8C61%u7684%uFF0C%u76F8%u5BF9%u5177%u4F53%u7684%u5B9E%u73B0%u4EE3%u7801%u8F83%u4E3A%u7A33%u5B9A%u3002%u800C%u88AB%u4F9D%u8D56%u7684%u4EE3%u7801%u5E94%u8BE5%u5C3D%u53EF%u80FD%u4FDD%u6301%u7A33%u5B9A%uFF0C%u8FD9%u6837%u57FA%u4E8E%u4E4B%u4E0A%u7684%u4EE3%u7801%u624D%u4E0D%u4F1A%u56E0%u4E3A%u4F9D%u8D56%u7684%u6539%u52A8%u800C%u6539%u52A8%u3002%0A%0A%u4E0B%u9762%u5217%u51FA%u51E0%u79CD%u6211%u5728%u5B9E%u9645%u5199test%20case%u65F6%u9047%u5230%u7684%u60C5%u51B5%uFF0C%u5728gmock%u4E2D%u7684%u89E3%u51B3%u65B9%u6848%u3002%0A%0A%23%23%23%20%u6309%u7167%u51FD%u6570%u53C2%u6570%u8FD4%u56DE%u7ED3%u679C%20-%20Fake%0A%u4F8B%u5982%3A%0A%60%60%60%0Aclass%20A%0A%7B%0A%09virtual%20int%20func%28int%20a%2C%20int%20b%29%3B%0A%7D%0A%0AEXPECT_CALL%28mockA%2C%20func%28%29%29%0A%09.WillRepeatedly%28a+b%29%3B%0A%60%60%60%0Agmock%u4E2D%u53EF%u4EE5%u8FD9%u6837%u505A%uFF1AUsing%20Functions/Methods/Functors%20as%20Actions%0A%60%60%60%0Ausing%20%3A%3Atesting%3A%3A_%3B%0Ausing%20%3A%3Atesting%3A%3AInvoke%3B%0Aclass%20MockFoo%20%3A%20public%20Foo%20%7B%0Apublic%3A%0A%09MOCK_METHOD2%28Sum%2C%20int%28int%20x%2C%20int%20y%29%29%3B%0A%09MOCK_METHOD1%28ComplexJob%2C%20bool%28int%20x%29%29%3B%0A%7D%3B%0Aint%20CalculateSum%28int%20x%2C%20int%20y%29%20%7B%20return%20x%20+%20y%3B%20%7D%0Aclass%20Helper%20%7B%0Apublic%3A%0A%09bool%20ComplexJob%28int%20x%29%3B%0A%7D%3B%0A...%0AMockFoo%20foo%3B%0AHelper%20helper%3B%0AEXPECT_CALL%28foo%2C%20Sum%28_%2C%20_%29%29%0A%09.WillOnce%28Invoke%28CalculateSum%29%29%3B%0AEXPECT_CALL%28foo%2C%20ComplexJob%28_%29%29%0A%09.WillOnce%28Invoke%28%26helper%2C%20%26Helper%3A%3AComplexJob%29%29%3B%0Afoo.Sum%285%2C%206%29%3B%20//%20Invokes%20CalculateSum%285%2C%206%29.%0Afoo.ComplexJob%2810%29%3B%20//%20Invokes%20helper.ComplexJob%2810%29%3B%0A%60%60%60%0A%0A%23%23%23%20Mock%20non-virtual%u51FD%u6570%0A%60%60%60%0A//%20A%20simple%20packet%20stream%20class.%20None%20of%20its%20members%20is%20virtual.%0Aclass%20ConcretePacketStream%20%7B%0A%09public%3A%0A%09%09void%20AppendPacket%28Packet*%20new_packet%29%3B%0A%09%09const%20Packet*%20GetPacket%28size_t%20packet_number%29%20const%3B%0A%09%09size_t%20NumberOfPackets%28%29%20const%3B%0A%09%09...%0A%7D%3B%0A//%20A%20mock%20packet%20stream%20class.%20It%20inherits%20from%20no%20other%2C%20but%20defines%0A//%20GetPacket%28%29%20and%20NumberOfPackets%28%29.%0Aclass%20MockPacketStream%20%7B%0A%09public%3A%0A%09%09MOCK_CONST_METHOD1%28GetPacket%2C%20const%20Packet*%28size_t%20packet_number%29%29%3B%0A%09%09MOCK_CONST_METHOD0%28NumberOfPackets%2C%20size_t%28%29%29%3B%0A%09%09...%0A%7D%0A%0Atemplate%20%3Cclass%20PacketStream%3E%0Avoid%20CreateConnection%28PacketStream*%20stream%29%20%7B%20...%20%7D%0Atemplate%20%3Cclass%20PacketStream%3E%0Aclass%20PacketReader%20%7B%0A%09public%3A%0A%09void%20ReadPackets%28PacketStream*%20stream%2C%20size_t%20packet_num%29%3B%0A%7D%3B%0A%0AMockPacketStream%20mock_stream%3B%0AEXPECT_CALL%28mock_stream%2C%20...%29...%3B%0A..%20set%20more%20expectations%20on%20mock_stream%20...%0APacketReader%3CMockPacketStream%3E%20reader%28%26mock_stream%29%3B%0A...%20exercise%20reader%20...%0A%60%60%60%0A%u4E3A%u4EC0%u4E48%u8981%u8FD9%u4E48%u505A%uFF1F%0A%u56E0%u4E3A%u53EA%u80FD%u8FD9%u4E48%u505A%u3002%u666E%u901A%u7684mock%uFF0C%u8981%u901A%u8FC7%u7EE7%u627F%u88AB%u6D4B%u8BD5%u7C7B%uFF0C%u5E76%u91CD%u5199virtual%u51FD%u6570%u6765%u5B9E%u73B0%u3002%u800C%u4E0A%u9762%u7684ConcretePacketStream%u548CMockPacketStream%u5E76%u4EFB%u4F55%u6CA1%u6709%u5173%u7CFB%uFF0C%u4E5F%u5C31%u662F%u8BF4%uFF0C%u5982%u679C%u4F20%u5165%u540E%u8005%u7684%u6307%u9488%uFF0C%u4E0D%u7528reinterpret_cast%u662F%u4E0D%u80FD%u8F6C%u6210%u524D%u8005%u7684%u6307%u9488%u7684%u3002%0A%u6240%u4EE5%u60F3%u4E00%u4E2A%u53D8%u901A%u7684%u529E%u6CD5%uFF0C%u7528%u6A21%u677F%u7C7B%u6765%u5B9A%u4E49%u88AB%u6D4B%u4EE3%u7801%uFF0C%u5728%u6D4B%u8BD5%u65F6%u4F20%u5165mock%u7C7B%uFF0C%u5728%u751F%u4EA7%u65F6%uFF0C%u4F20%u5165%u771F%u5B9E%u7C7B%u3002%0A%0A%23%23%23%20Mocking%20Side%20Effects%0A%60%60%60%0AEXPECT_CALL%28mutator%2C%20MutateInt%28_%29%29%0A%09.WillOnce%28DoAll%28SetArgPointee%3C0%3E%285%29%2C%20Return%28true%29%29%29%3B%0AEXPECT_CALL%28mutator%2C%20Mutate%28NotNull%28%29%2C%205%29%29%0A%09.WillOnce%28SetArrayArgument%3C0%3E%28values%2C%20values%20+%205%29%29%3B%0A%60%60%60%0A%u7B2C%u4E00%u4E2A%u5C06MutateInt%u7B2C%u4E00%u4E2A%u53C2%u6570%u6307%u9488%u6307%u5411%u7684int%uFF0C%u8BBE%u4E3A5%uFF0C%u5E76%u8FD4%u56DEtrue%u3002%0A%u7B2C%u4E8C%u4E2A%u5C06values%u6570%u7EC4%u7684%5B0%2C5%29%u62F7%u8D1D%u5230%u53C2%u65701%u6307%u5411%u7684%u5730%u5740%u3002%0A%u5982%u679C%u4ECD%u9700%u8981%u8FD4%u56DE%uFF0C%u5219%u7528DoAll%uFF0C%u5982%u4E0B%uFF1A%0A%60%60%60%0AEXPECT_CALL%28mutator%2C%20MutateInt%28_%29%29%0A%20%20%20%20%20%20.WillOnce%28DoAll%28SetArgPointee%3C0%3E%285%29%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Return%28true%29%29%29%3B%0A%60%60%60%0A%0A%23%23%23%20Selecting%20an%20Action%27s%20Arguments%0A%60%60%60%0Ausing%20%3A%3Atesting%3A%3A_%3B%0Ausing%20%3A%3Atesting%3A%3AInvoke%3B%0Abool%20MyIsVisibleInQuadrant1%28bool%20visible%2C%20const%20string%26%20name%2C%20int%20x%2C%20int%20y%2C%0Aconst%20map%3Cpair%3Cint%2C%20int%3E%2C%20double%3E%26%20weight%2C%0Adouble%20min_weight%2C%20double%20max_wight%29%20%7B%0A%09return%20IsVisibleInQuadrant1%28visible%2C%20x%2C%20y%29%3B%0A%7D.%0A..%0AEXPECT_CALL%28mock%2C%20Foo%28_%2C%20_%2C%20_%2C%20_%2C%20_%2C%20_%2C%20_%29%29%0A%09.WillOnce%28Invoke%28MyIsVisibleInQuadrant1%29%29%3B%20//%20Now%20it%20works.%0A%60%60%60%0A%u5B9A%u4E49%u81EA%u5DF1%u7684adaptor%20MyIsVisibleInQuadrant1%uFF0C%u6216%u8005%u7528gmock%u63D0%u4F9B%u7684%u65B9%u6CD5%u4F18%u96C5%u7684%u89E3%u51B3%u3002%0A%60%60%60%0Ausing%20%3A%3Atesting%3A%3A_%3B%0Ausing%20%3A%3Atesting%3A%3AInvoke%3B%0Ausing%20%3A%3Atesting%3A%3AWithArgs%3B%0A...%0AEXPECT_CALL%28mock%2C%20Foo%28_%2C%20_%2C%20_%2C%20_%2C%20_%2C%20_%2C%20_%29%29%0A.WillOnce%28WithArgs%3C0%2C%202%2C%203%3E%28Invoke%28IsVisibleInQuadrant1%29%29%29%3B%0A//%20No%20need%20to%20define%20your%20own%20adaptor.%0A%60%60%60%0A%0A%23%23%23%23%20Mocking%20Private%20or%20Protected%20Methods%0A%60%60%60%0Aclass%20Foo%20%7B%0A%09public%3A%0A%09%09...%0A%09%09virtual%20bool%20Transform%28Gadget*%20g%29%20%3D%200%3B%0A%09protected%3A%0A%09%09virtual%20void%20Resume%28%29%3B%0A%09private%3A%0A%09%09virtual%20int%20GetTimeOut%28%29%3B%0A%7D%3B%0Aclass%20MockFoo%20%3A%20public%20Foo%20%7B%0A%09public%3A%0A%09%09...%0A%09%09MOCK_METHOD1%28Transform%2C%20bool%28Gadget*%20g%29%29%3B%0A%09%09//%20The%20following%20must%20be%20in%20the%20public%20section%2C%20even%20though%20the%0A%09%09//%20methods%20are%20protected%20or%20private%20in%20the%20base%20class.%0A%09%09MOCK_METHOD0%28Resume%2C%20void%28%29%29%3B%0A%09%09MOCK_METHOD0%28GetTimeOut%2C%20int%28%29%29%3B%0A%7D%3B%0A%60%60%60%0A%3EC++%20allows%20a%20subclass%20to%20specify%20a%20different%20access%20level%20than%20the%20base%20class%20on%20a%20virtual%20function.%0A%0A%23%23%23%23%20Misc%0AKeep%20in%20mind%20that%20one%20doesn%27t%20have%20to%20verify%20more%20than%20one%20property%20in%20one%20test.%20In%20fact%2C%20it%27s%20a%20good%20style%20to%20verify%20only%20one%0Athing%20in%20one%20test.%20If%20you%20do%20that%2C%20a%20bug%20will%20likely%20break%20only%20one%20or%20two%20tests%20instead%20of%20dozens%0A%0AWhen%20it%27s%20being%20destroyed%2C%20your%20friendly%20mock%20object%20will%20automatically%20verify%20that%20all%20expectations%20on%20it%20have%20been%20satisfied%2C%0Aand%20will%20generate%20Google%20Test%20failures%20if%20not.%20%0A%0ACurrently%20these%20are%20only%20platforms%20that%20support%20the%20pthreads%20library%20%28this%20includes%20Linux%20and%20Mac%29.%20%0A%0A%u52A0%u4E0A%u547D%u4EE4%u884C%u53C2%u6570--gmock_verbose%3Dinfo%u53EF%u4EE5%u663E%u793A%u6240%u6709EXPECT%5C_CALL%u7684%u5177%u4F53%u8C03%u7528%u60C5%u51B5%u3002%0A%0A%23%23%20Some%20useful%20tips%20in%20gtest%0A%23%23%23%20Selecting%20Tests%0A%3E%20If%20you%20set%20the%20**GTEST_FILTER**%20environment%20variable%20or%20the%20**--gtest_filter**%20flag%20to%20a%20filter%20string%2C%20Google%20Test%20will%20only%20run%20the%20tests%20whose%20full%20names%20%28in%20the%20form%20of%20TestCaseName.TestName%29%20match%20the%20filter.%0A%3E%20The%20format%20of%20a%20filter%20is%20a%20%27%3A%27-separated%20list%20of%20wildcard%20patterns%20%28called%20the%20positive%20patterns%29%20optionally%20followed%20by%20a%20%27-%27%20and%20another%20%27%3A%27-separated%20pattern%20list%20%28called%20the%20negative%20patterns%29.%0A%0A-%20./foo_test%20Has%20no%20flag%2C%20and%20thus%20runs%20all%20its%20tests.%0A-%20./foo_test%20--gtest_filter%3D*%20Also%20runs%20everything%2C%20due%20to%20the%20single%20match-everything%20*%20value.%0A-%20./foo_test%20--gtest_filter%3DFooTest.*%20Runs%20everything%20in%20test%20case%20FooTest.%0A-%20./foo_test%20--gtest_filter%3D*Null*%3A*Constructor*%20Runs%20any%20test%20whose%20full%20name%20contains%20either%20%22Null%22%20or%20%22Constructor%22.%0A-%20./foo_test%20--gtest_filter%3D-*DeathTest.*%20Runs%20all%20non-death%20tests.%0A-%20./foo_test%20--gtest_filter%3DFooTest.*-FooTest.Bar%20Runs%20everything%20in%20test%20case%20FooTest%20except%20FooTest.Bar%0A%0ATemporarily%20Disabling%20Tests%0A%60%60%60%0A//%20Tests%20that%20Foo%20does%20Abc.%0ATEST%28FooTest%2C%20DISABLED_DoesAbc%29%20%7B%20...%20%7D%0A%0Aclass%20DISABLED_BarTest%20%3A%20public%20%3A%3Atesting%3A%3ATest%20%7B%20...%20%7D%3B%0A%0A//%20Tests%20that%20Bar%20does%20Xyz.%0ATEST_F%28DISABLED_BarTest%2C%20DoesXyz%29%20%7B%20...%20%7D%0A%60%60%60%0A%0ATemporarily%20Enabling%20Disabled%20Tests%0A%3Ejust%20invoke%20the%20test%20program%20with%20the%20**--gtest_also_run_disabled_tests**%20flag%20or%20set%20the%20**GTEST_ALSO_RUN_DISABLED_TESTS**%20environment%20variable%20to%20a%20value%20other%20than%200.%0A%0A%23%23%23%20Repeating%20the%20Tests%0A%7C%20%20%20%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%7C%20%3A--------%3A%20%7C%20%3A--------------%3A%20%7C%20%3A--------%3A%20%7C%0A%7C%5C%24%20foo_test%20--gtest_repeat%3D1000%20%7C%09Repeat%20foo_test%201000%20times%20and%20don%27t%20stop%20at%20failures.%7C%0A%7C%5C%24%20foo_test%20--gtest_repeat%3D-1%09%7CA%20negative%20count%20means%20repeating%20forever.%7C%0A%7C%5C%24%20foo_test%20--gtest_repeat%3D1000%20--gtest_break_on_failure%09%7C%20Repeat%20foo_test%201000%20times%2C%20stopping%20at%20the%20first%20failure.%20This%20is%20especially%20useful%20when%20running%20under%20a%20debugger%3A%20when%20the%20testfails%2C%20it%20will%20drop%20into%20the%20debugger%20and%20you%20can%20then%20inspect%20variables%20and%20stacks.%7C%0A%7C%24%20foo_test%20--gtest_repeat%3D1000%20--gtest_filter%3DFooBar%09%7C%20Repeat%20the%20tests%20whose%20name%20matches%20the%20filter%201000%20times.%7C