c++ - Google Mock:测试对象的某个属性
问题描述
我尝试按照 Google Mock CookBook 中提供的示例定义自定义匹配器。
代码如下
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace std;
using ::testing::MatcherInterface;
using ::testing::MatchResultListener;
using ::testing::Matcher;
using ::testing::_;
using ::testing::AtLeast;
using ::testing::Invoke;
class Foo2;
class Foo2
{
public:
virtual int bar() const
{
return 4;
}
virtual int baz() const
{
return 5;
}
virtual void DoThis (Matcher<const Foo2&> pFunc)
{
std::cout << "Foo:DoThis" << std::endl;
}
virtual void DoThat(int)
{
std::cout << "Foo:DoThat" << std::endl;
}
virtual ~Foo2()
{
}
};
class BarPlusBazEqMatcher : public MatcherInterface<const Foo2&> {
public:
explicit BarPlusBazEqMatcher(int expected_sum)
: expected_sum_(expected_sum) {}
virtual bool MatchAndExplain(const Foo2& foo,
MatchResultListener* listener) const {
return (foo.bar() + foo.baz()) == expected_sum_;
}
virtual void DescribeTo(::std::ostream* os) const {
*os << "bar() + baz() equals " << expected_sum_;
}
virtual void DescribeNegationTo(::std::ostream* os) const {
*os << "bar() + baz() does not equal " << expected_sum_;
}
virtual ~BarPlusBazEqMatcher()
{
}
private:
const int expected_sum_;
};
inline Matcher<const Foo2&> BarPlusBazEq(int expected_sum) {
return MakeMatcher(new BarPlusBazEqMatcher(expected_sum));
}
class MockFoo2 : public Foo2 {
public:
MOCK_METHOD1(DoThis,void(Matcher<const Foo2&>));
MOCK_METHOD1(DoThat, void(int));
};
TEST(MockMatcher, Matcher)
{
MockFoo2 mockF;
EXPECT_CALL(mockF, DoThis(BarPlusBazEq(5)));
}
当我尝试编译上述代码时,会产生以下编译错误
*..\..gtest\gtest.h:9160:60: error: no match for 'operator==' (operand types are 'const testing::Matcher<const Foo2&>' and 'const testing::Matcher<const Foo2&>')
bool operator()(const A& a, const B& b) const { return a == b; }*
~~^~~~
*..\..gtest\gtest.h:14096:13: note: candidate: bool testing::internal::operator==(testing::internal::faketype, testing::internal::faketype)
inline bool operator==(faketype, faketype) { return true; }*
*..\..gtest\gtest.h:14096:13: note: no known conversion for argument 1 from 'const testing::Matcher<const Foo2&>' to 'testing::internal::faketype'*
如何解决这些错误?
解决方案
匹配器应该只在你的测试方法中使用,而不是在类定义中。此BarPlusBazEq
示例中的 期望匹配类型为 的函数参数const &Foo2
。所以DoThis()
需要定义为采用该参数类型的函数:
class Foo2 {
public:
virtual int bar() const { return 4; }
virtual int baz() const { return 5; }
virtual void DoThis(const Foo2 &foo) {
std::cout << "Foo2:DoThis" << std::endl;
}
virtual ~Foo2() {}
};
class MockFoo2 : public Foo2 {
public:
MOCK_METHOD1(DoThis, void(const Foo2&));
};
class BarPlusBazEqMatcher : public MatcherInterface<const Foo2&> {
public:
explicit BarPlusBazEqMatcher(int expected_sum)
: expected_sum_(expected_sum) {}
virtual bool MatchAndExplain(const Foo2& foo,
MatchResultListener* listener) const {
return (foo.bar() + foo.baz()) == expected_sum_;
}
virtual void DescribeTo(::std::ostream* os) const {
*os << "bar() + baz() equals " << expected_sum_;
}
virtual void DescribeNegationTo(::std::ostream* os) const {
*os << "bar() + baz() does not equal " << expected_sum_;
}
virtual ~BarPlusBazEqMatcher() {}
private:
const int expected_sum_;
};
inline Matcher<const Foo2&> BarPlusBazEq(int expected_sum) {
return MakeMatcher(new BarPlusBazEqMatcher(expected_sum));
}
TEST(MockMatcher, Matcher)
{
MockFoo2 mock;
EXPECT_CALL(mock, DoThis(BarPlusBazEq(9)));
// You also have to do something to trigger the expected call...
Foo2 foo2;
mock.DoThis(foo2);
}
顺便说一句,您还可以避免定义完整接口的麻烦,并使用简单的自定义匹配器完成相同的事情,如下所示:
MATCHER_P(BarPlusBazEq, expected_sum, "") { return arg.bar() + arg.baz() == expected_sum; }
推荐阅读
- python - PyQT Graph -- 图形显示巨大
- c# - 如何从 MVC 5 中的 FileResult 和锚标记下载文件?
- python-3.x - 如何在熊猫中将一周的第一天设置为星期二
- typescript - 打字稿和传单 - 地图缩放超出范围
- sql-server - SQL Server 导入和导出向导 - 更改并发设置
- ios - UISCROLLVIEW 上方的圆形按钮
- apache-nifi - 运行一次 nifi flow 并在完成时通知我
- c# - 在 C# 中将可变大小的十六进制字符串转换为有符号数(可变大小字节)
- shell - Unix shell sh 从 setA 获取 SetB 中缺少的项目
- operating-system - 用户进程是否可以控制分页?