想象一下,我们有一个单元测试,它首先执行一个我们希望函数someFunc
不会被调用的代码序列,然后执行一个我们希望该函数只被调用一次的代码序列。使用HippoMocks,我们可以这样写:
#include <hippomocks.h>
void someFunc (void)
{
}
int main (int argc, char ** argv)
{
MockRepository mocks;
mocks.autoExpect = false;
mocks.NeverCallFunc(someFunc); // line 27
/* some testing code ... */
/* ... in the course of which someFunc does not get called ... */
mocks.ExpectCallFunc(someFunc); // line 33
/* other testing code ... */
someFunc();
/* ... in the course of which someFunc got called */
return 0;
}
但是,在 Windows 上运行上述代码片段(使用 Cygwin 工具链编译)时,HippoMocks::ExpectationException
会抛出 a:
terminate called after throwing an instance of 'HippoMocks::ExpectationException'
what(): Function someFunc() called with mismatching expectation!
Expectations set:
../main.cpp(33) Expectation for someFunc() on the mock at 0x0 was not satisfied.
Functions explicitly expected to not be called:
../main.cpp(27) Result set for someFunc() on the mock at 0x0 was used.
所以我想知道...
... (1),如果 HippoMocks 不是为处理这种情况而设计的。期望someFunc
被调用(第 33 行)不会取代相应模拟存储库中的先前期望吗?
...(2),为什么没有满足第二个期望(第 33 行),因为someFunc
明确地被调用了。如果有的话,我会期望第一个期望(第 27 行)没有得到满足?
有趣的是,事情正好相反。以下代码段运行没有任何问题:
#include <hippomocks.h>
void someFunc (void)
{
}
int main (int argc, char ** argv)
{
MockRepository mocks;
mocks.autoExpect = false;
mocks.ExpectCallFunc(someFunc); // line 27
/* some testing code ... */
someFunc();
/* ... in the course of which someFunc got called */
mocks.NeverCallFunc(someFunc); // line 33
/* other testing code ... */
/* ... in the course of which someFunc does not get called ... */
/* someFunc(); */
return 0;
}
此外,如果someFunc
在第二个片段中的第二个期望后面插入了对的调用(如注释中所示),这将被检测到并报告为违反 HippoMocks 的“从不调用”期望,正如人们所期望的那样:
terminate called after throwing an instance of 'HippoMocks::ExpectationException'
what(): Function someFunc() called with mismatching expectation!
Expectations set:
../main.cpp(27) Expectation for someFunc() on the mock at 0x0 was satisfied.
Functions explicitly expected to not be called:
../main.cpp(33) Result set for someFunc() on the mock at 0x0 was used.
HippoMocks 专家的任何帮助将不胜感激......