谷歌模拟测试一个类的真实行为
Google Mock to test the real behaviors of a class
我是谷歌测试和谷歌模拟的新手,所以我仍然有点困惑。我只是试着用整数加法、乘法和除法来实现一个简单的计算器,并为它创建一个模仿真实行为的模型。我该如何修复它,或者我哪里做错了?
你还可以向我解释一下,我如何才能确定它是在嘲笑原始类,而不是直接调用原始类?谢谢你。
这是CBasicMath.hpp:
#ifndef BASIC_MATH_HPP__
#define BASIC_MATH_HPP__
class CBasicMath
{
public:
CBasicMath(){}
virtual ~CBasicMath() {}
virtual int Addition(int x, int y);
virtual int Multiply(int x, int y);
virtual int Divide(int x, int y);
};
#endif //BASIC_MATH_HPP__
这是CBasicMath.cpp:
#include "CBasicMath.hpp"
int CBasicMath::Addition(int x, int y)
{
return (x + y);
}
int CBasicMath::Multiply(int x, int y)
{
return (x * y);
}
int CBasicMath::Divide(int x, int y)
{
return (x / y);
}
这里是mock_basic_test.cpp:
#include "gmock/gmock.h"
#include "CBasicMath.cpp"
using ::testing::_;
using ::testing::AtLeast;
using ::testing::Invoke;
class MockBasicTest : public CBasicMath {
public:
MockBasicTest() {
// By default, all calls are delegated to the real object.
ON_CALL(*this, Addition(_))
.WillByDefault(Invoke(&real_, &CBasicMath::Addition));
ON_CALL(*this, Multiply(_))
.WillByDefault(Invoke(&real_, &CBasicMath::Multiply));
ON_CALL(*this, Divide(_))
.WillByDefault(Invoke(&real_, &CBasicMath::Divide));
}
MOCK_METHOD2(Addition, int(int x, int y));
MOCK_METHOD2(Multiply, int(int x, int y));
MOCK_METHOD2(Divide, int(int x, int y));
private:
CBasicMath real_;
};
这是TestBasicMath:
#include "mock_basic_test.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
class BasicMathTest : public ::testing::Test {
protected:
BasicMathTest() {}
virtual ~BasicMathTest() {}
virtual void SetUp() {
mTestObj = new CBasicMath();
}
virtual void TearDown() {
delete mTestObj;
}
CBasicMath *mTestObj;
};
TEST_F(BasicMathTest, testAddition) {
MockBasicTest basictest;
EXPECT_CALL(basictest, Addition(2,3))
.Times(1);
EXPECT_EQ(5,basictest.Addition(2,3));
}
TEST_F(BasicMathTest, testMultiply) {
EXPECT_EQ(6,mTestObj->Multiply(2,3));
}
TEST_F(BasicMathTest, testDivide) {
EXPECT_EQ(6,mTestObj->Divide(6,1));
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
它给了我以下三个功能的错误:
In file included from /home/gmock-1.7.0/include/gmock/gmock-generated-function-mockers.h:43:0,
from /home/gmock-1.7.0/include/gmock/gmock.h:61,
from mock_basic_test.h:1,
from TestBasicMath_GoogleTest.cpp:1:
mock_basic_test.h: In constructor ‘MockBasicTest::MockBasicTest()’:
mock_basic_test.h:12:30: error: no matching function for call to ‘MockBasicTest::gmock_Addition(const testing::internal::AnythingMatcher&)’
ON_CALL(*this, Addition(_))
^
mock_basic_test.h:12:30: note: candidate is:
In file included from /home/gmock-1.7.0/include/gmock/gmock.h:61:0,
from mock_basic_test.h:1,
from TestBasicMath_GoogleTest.cpp:1:
mock_basic_test.h:19:2: note: testing::internal::MockSpec<int(int, int)>&MockBasicTest::gmock_Addition(const testing::Matcher<int>&, const testing::Matcher<int>&)
MOCK_METHOD2(Addition, int(int x, int y));
^
mock_basic_test.h:19:2: note: candidate expects 2 arguments, 1 provided
再次感谢您的帮助。
在您的模拟类构造函数代码中
ON_CALL(*this, Addition(_))
.WillByDefault(Invoke(&real_, &CBasicMath::Addition));
匹配器的数量(_
)需要与为函数签名定义的参数数量相同:
ON_CALL(*this, Addition(_,_))
// ^^ Add an additional matcher
.WillByDefault(Invoke(&real_, &CBasicMath::Addition));
// ...
MOCK_METHOD2(Addition, int(int x, int y));
// ^^^^^ ^^^^^ here you have 2 parameters need matching
只有当可以默认构造"T real_"时,这才有效。此外,您还可以在mock中获得T的另一个副本,这是不需要的。
Imho,你可以这样做,因为你可以通过任何一种方式访问基类:
ON_CALL(*this, Addition(_))
.WillByDefault(Invoke(*this, &CBasicMath::Addition));
通过这种方式,调用只是被delgated到基类。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 激励'inline'说明符的真实世界示例?
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 从链接列表c++中删除一个项目
- C - 如何编写一个函数,返回真实有价值函数的导数,而不是导数的值
- 如何编写一个通用函数,该通用函数在没有任何条件和条件的情况下工作(无论是真实和错误)
- 给定一个大小的布尔数组,这都是真实的,我将如何仅使用递归将所有值设置为false
- 如何在C++中将像"u94b1"这样的字符串转换为一个真实字符?
- 你能给我一个内存泄漏的真实工作示例吗?
- 谷歌模拟测试一个类的真实行为