http://koalaquwei.ucoz.com/_ld/0/77_Google_Mock_Coo.pdf
原文链接:http://quweiprotoss.blog.163.com/blog/static/40882883201222721548449/
Google C++ Mocking Cookbook
Version: 0.32
作者:Adrian Alexander
译者:Koala++ /屈伟
Using Matchers in Google Test Assertions
因为Matchers基本上就是Predicates,所以这就提供了一种在Google Test中使用它们的好方法。它叫ASSERT_THAT
和EXPECT_THAT
:
ASSERT_THAT(value, matcher); // Asserts that value matches matcher.
EXPECT_THAT(value, matcher); // The non-fatal version.
例如,在Google Test中你可以写:
#include"gmock/gmock.h"
using ::testing::AllOf;
using ::testing::Ge;
using ::testing::Le;
using ::testing::MatchesRegex;
using ::testing::StartsWith;
//...
EXPECT_THAT(Foo(), StartsWith("Hello"));
EXPECT_THAT(Bar(), MatchesRegex("Line \\d+"));
ASSERT_THAT(Baz(), AllOf(Ge(5), Le(10)));
上面的代码(正如你所猜测的 )执行Foo(),Bar(),和Baz(),并验证:
- Foo()返回一个以”Hello”开头的字符串。
- Bar()返回一个匹配”Line\d+”的正则表达式。
- Baz()返回一个在[5, 10]区间内的数字。
这些宏带来的好处是它们读起来像是英语。它们也会产生提示消息。比如,如果第一个EXPECT_THAT失败,消息会类似下面的:
Value of: Foo()
Actual: "Hi, world!"
Expected: starts with "Hello"
网友评论