google mock分享(全网最全最好的gmock文档,没有之一)
StrNe(string) | 参数不等于string |
容器的匹配
很多STL的容器的比较都支持==这样的操作,对于这样的容器可以使用上述的Eq(container)来比较。但如果你想写得更为灵活,可以使用下面的这些容器匹配方法:
Contains(e) | 在method的形参中,只要有其中一个元素等于e |
Each(e) | 参数各个元素都等于e |
ElementsAre(e0, e1, …, en) | 形参有n+1的元素,并且挨个匹配 |
ElementsAreArray(array) 或者ElementsAreArray(array, count) | 和ElementsAre()类似,除了预期值/匹配器来源于一个C风格数组 |
ContainerEq(container) | 类型Eq(container),就是输出结果有点不一样,这里输出结果会带上哪些个元素不被包含在另一个容器中 |
Pointwise(m, container) |
上述的一些匹配器都比较简单,我就随便打包举几最简单的例子演示一下吧: 我稍微修改一下之前的Foo.h和MockFoo.h, MockFoo.h 增加了2个方法
- #ifndef MOCKFOO_H_
- #define MOCKFOO_H_
- #include <gmock/gmock.h>
- #include <string>
- #include <vector>
- #include "FooInterface.h"
- namespace seamless {
- class MockFoo: public FooInterface {
- public:
- MOCK_METHOD0(getArbitraryString, std::string());
- MOCK_METHOD1(setValue, void(std::string& value));
- MOCK_METHOD2(setDoubleValues, void(int x, int y));
- };
- } // namespace seamless
- #endif // MOCKFOO_H_
FooMain.h
- #include <cstdlib>
- #include <gmock/gmock.h>
- #include <iostream>
- #include <string>
- #include "MockFoo.h"
- using namespace seamless;
- using namespace std;
- using ::testing::Assign;
- using ::testing::Eq;
- using ::testing::Ge;
- using ::testing::Return;
- int main(int argc, char** argv) {
- ::testing::InitGoogleMock(&argc, argv);
- string value = "Hello World!";
- MockFoo mockFoo;
- EXPECT_CALL(mockFoo, setValue(testing::_));
- mockFoo.setValue(value);
- // 这里我故意犯错
- EXPECT_CALL(mockFoo, setDoubleValues(Eq(1), Ge(1)));
- mockFoo.setDoubleValues(1, 0);
- return EXIT_SUCCESS;
- }
- 第22行,让setValue的形参可以传入任意参数
- 另外,我在第26~27行故意犯了个错(为了说明上述这些匹配器的作用),我之前明明让setDoubleValues第二个参数得大于等于1,但我实际传入时却传入一个0。这时程序运行时就报错了:
Unexpected mock function call – returning directly.
Function call: setDoubleValues(1, 0)
Google Mock tried the following 1 expectation, but it didn't match:
FooMain.cc:35: EXPECT_CALL(mockFoo, setDoubleValues(Eq(1), Ge(1)))…
Expected arg #1: is >= 1
Actual: 0
Expected: to be called once
Actual: never called – unsatisfied and active
FooMain.cc:35: Failure
Actual function call count doesn't match EXPECT_CALL(mockFoo, setDoubleValues(Eq(1), Ge(1)))…
Expected: to be called once
Actual: never called – unsatisfied and active
上述的那些匹配器都比较简单,下面我们来看看那些比较复杂的匹配吧。
成员匹配器
Field(&class::field, m) | argument.field (或 argument->field, 当argument是一个指针时)与匹配器m匹配, 这里的argument是一个class类的实例. |
Key(e) | 形参(argument)比较是一个类似map这样的容器,然后argument.first的值等于e |
Pair(m1, m2) | 形参(argument)必须是一个pair,并且argument.first等于m1,argument.second等于m2. |
Property(&class::property, m) | argument.property()(或argument->property(),当argument是一个指针时)与匹配器m匹配, 这里的argument是一个class类的实例. |
还是举例说明一下:
- TEST(TestField, Simple) {
- MockFoo mockFoo;
- Bar bar;
- EXPECT_CALL(mockFoo, get(Field(&Bar::num, Ge(0)))).Times(1);
- mockFoo.get(bar);
- }
- int main(int argc, char** argv) {
- ::testing::InitGoogleMock(&argc, argv);
- return RUN_ALL_TESTS();
- }
上面这个是正确的例子,我们为了说明Field的作用,传入一个bar.num = -1试试。
- TEST(TestField, Simple) {
- MockFoo mockFoo;
- Bar bar;
- bar.num = -1;
- EXPECT_CALL(mockFoo, get(Field(&Bar::num, Ge(0)))).Times(1);
- mockFoo.get(bar);
- }
[----------] Global test environment set-up.
[----------] 1 test from TestField
[ RUN ] TestField.Simple
unknown file: Failure
Unexpected mock function call – returning directly.
Function call: get(@0xbff335bc 4-byte object
Google Mock tried the following 1 expectation, but it didn't match:
FooMain.cc:34: EXPECT_CALL(mockFoo, get(Field(&Bar::num, Ge(0))))…
Expected arg #0: is an object whose given field is >= 0
Actual: 4-byte object
Expected: to be called once
Actual: never called – unsatisfied and active
FooMain.cc:34: Failure
Actual function call count doesn't match EXPECT_CALL(mockFoo, get(Field(&Bar::num, Ge(0))))…
Expected: to be called once
Actual: never called – unsatisfied and active
[ FAILED ] TestField.Simple (0 ms)
[----------] 1 test from TestField (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] TestField.Simple
1 FAILED TEST
匹配函数或函数对象的返回值
ResultOf(f, m) | f(argument) 与匹配器m匹配, 这里的f是一个函数或函数对象. |
指针匹配器
Pointee(m) | argument (不论是智能指针还是原始指针) 指向的值与匹配器m匹配. |
复合匹配器
AllOf(m1, m2, …, mn) | argument 匹配所有的匹配器m1到mn |
AnyOf(m1, m2, …, mn) | argument 至少匹配m1到mn中的一个 |
Not(m) | argument 不与匹配器m匹配 |
- EXPECT_CALL(foo, DoThis(AllOf(Gt(5), Ne(10))));
- 传入的参数必须 >5 并且 <= 10
- EXPECT_CALL(foo, DoThat(Not(HasSubstr("blah")), NULL));
- 第一个参数不包含“blah”这个子串
基数(Cardinalities)
基数用于Times()中来指定模拟函数将被调用多少次|
AnyNumber() | 函数可以被调用任意次. |
AtLeast(n) | 预计至少调用n次. |
AtMost(n) | 预计至多调用n次. |
Between(m, n) | 预计调用次数在m和n(包括n)之间. |
Exactly(n) 或 n | 预计精确调用n次. 特别是, 当n为0时,函数应该永远不被调用. |
行为(Actions)
Actions(行为)用于指定Mock类的方法所期望模拟的行为:比如返回什么样的值、对引用、指针赋上怎么样个值,等等。 值的返回
Return() | 让Mock方法返回一个void结果 |
Return(value) | 返回值value |
ReturnNull() | 返回一个NULL指针 |
ReturnRef(variable) | 返回variable的引用. |
ReturnPointee(ptr) | 返回一个指向ptr的指针 |
另一面的作用(Side Effects)
Assign(&variable, value) | 将value分配给variable |
使用函数或者函数对象(Functor)作为行为
Invoke(f) | 使用模拟函数的参数调用f, 这里的f可以是全局/静态函数或函数对象. |
Invoke(object_pointer, &class::method) | 使用模拟函数的参数调用object_pointer对象的mothod方法. |
复合动作
DoAll(a1, a2, …, an) | 每次发动时执行a1到an的所有动作. |
IgnoreResult(a) | 执行动作a并忽略它的返回值. a不能返回void. |
这里我举个例子来解释一下DoAll()的作用,我个人认为这个DoAll()还是挺实用的。例如有一个Mock方法:
- virtual int getParamter(std::string* name, std::string* value) = 0
对于这个方法,我这回需要操作的结果是将name指向value的地址,并且得到方法的返回值。
类似这样的需求,我们就可以这样定义期望过程:
- TEST(SimpleTest, F1) {
- std::string* a = new std::string("yes");
- std::string* b = new std::string("hello");
- MockIParameter mockIParameter;
- EXPECT_CALL(mockIParameter, getParamter(testing::_, testing::_)).Times(1).\
- WillOnce(testing::DoAll(testing::Assign(&a, b), testing::Return(1)));
- mockIParameter.getParamter(a, b);
- }
序列(Sequences)
默认时,对于定义要的期望行为是无序(Unordered)的,即当我定义好了如下的期望行为:
- MockFoo mockFoo;
- EXPECT_CALL(mockFoo, getSize()).WillOnce(Return(1));