需要谷歌模拟帮助,
Google Mock Help Required,
我有两个类。
class SomeClass
{
public:
int SomeFunction()
{
return 5;
}
};
class AnotherClass
{
public:
int AnotherFunction(SomeClass obj)
{
return obj.SomeFunction();
}
};
我为SomeClass上了一堂模拟课。
class MockSomeClass : public SomeClass
{
public:
MOCK_METHOD0(SomeFunction, int());
};
现在我想在单元测试中,当我调用AnotherClass.AnotherFunction时,我会得到自己选择的结果。AnotherFunction使用SomeClass.SomeFunction()的函数。我嘲笑过SomeClass。我已经设置了当模拟对象的函数调用它时返回10。但当我运行单元测试时,它返回5(原始函数)。我该怎么办?下面是我写的单元测试。
[TestMethod]
void TestMethod1()
{
MockSomeClass mock;
int expected = 10;
ON_CALL(mock, SomeFunction()).WillByDefault(Return(expected));
AnotherClass realClass;
int actual = realClass.AnotherFunction(mock);
Assert::AreEqual(expected, actual);
};
我使用的是visualstudio2008和gmock1.6.0。我在做什么不对。在realClass.AnotherFunction上,我想要mock的mock输出。SomeFunction()。
问题是SomeClass::SomeFunction(…)不是虚拟的,让它成为虚拟的,它就会工作。
更新:
还有一个更根本的错误导致它失败,那就是的方法签名
int AnotherFunction(SomeClass obj)
它创建了一个新的SomeClass对象实例,从而导致调用普通的SomeFunction,您应该将对模拟类的引用作为参数传递。
int AnotherFunction(SomeClass* obj)
并使用调用它
int actual = realClass.AnotherFunction(&mock);
相关文章:
- 如何使用Google Mock来模拟gettimeofday()
- 需要帮助设置在C++中使用的Potrace
- 在指针的帮助下,文本文件中单词的频率
- G锁定铸造到基础上会释放模拟行为
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 如何在Qbutton的帮助下更改Q对话框的宽度
- 需要帮助将结构数组传递给函数
- 落砂模拟碰撞检测C++和SFML
- 在gtest.中使用fff.h模拟系统API
- 谷歌模拟和覆盖关键字
- 用C#中的并集模拟C++嵌套结构
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 在模拟器中使用并集来模拟CPU寄存器有多合适
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 有人可以帮助我处理正则表达式吗?
- 需要谷歌模拟帮助,
- 需要帮助足球模拟