谷歌模拟:测试对象的某个属性
Google Mock:Testing a Certain Property of an Object
我尝试按照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)));
}
当我尝试编译上述代码时,但生成以下编译错误
*....gtestgtest.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; }*
~~^~~~
*....gtestgtest.h:14096:13: note: candidate: bool testing::internal::operator==(testing::internal::faketype, testing::internal::faketype)
inline bool operator==(faketype, faketype) { return true; }*
*....gtestgtest.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; }
相关文章:
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- C++ 在堆栈中包含多态属性的类对象存储
- 为什么我的 DeviceInformation 对象没有 System.Devices.InterfaceClassGuid 属性?
- 私有对象的互斥体 - 全局与属性
- 如何从传递到对象的对象内部访问对象的类属性?
- 从 lambda 返回的对象将丢失属性值
- 是否可以在C++中移动临时对象的属性?
- 如何通过对象的类属性删除对象,并返回其对象值?
- 类对象属性是否可以调用返回属性应具有的值的方法C++?
- C++对象作为 Objective-C++ 属性
- 如何在 C++ 中使用提升属性树从 JSON 文件中读取对象数组
- 对象属性同级访问
- 如何访问 JSCValue 对象的属性
- 从基于属性的列表中返回随机对象
- 迭代 Napi::对象属性(键)在 Node.js C++ N-API 中
- 返回对象会丢失属性
- 根据 C++ 中的属性对对象类进行分区的优化方法
- 基于对象的两个属性的自定义比较器会引发异常
- 创建新对象并立即为其设置属性时出现编译器错误
- 复合属性对象,当所有子对象都更改时,它禁止多个信号发射