我有2节课。
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);
};
我正在使用 Visual Studio 2008 和 gmock 1.6.0。我在做什么错。在 realClass.AnotherFunction 上,我想要 mock.SomeFunction() 的模拟输出。