someFunc
関数が呼び出されないことが予想されるコードのシーケンスを最初に実行し、次にその関数が 1 回だけ呼び出されることが予想されるコードのシーケンスを実行する単体テストがあるとします。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
にスローされます。
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)、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
(コメントに示されているように) 2 番目のスニペットの 2 番目の期待値の後ろに への呼び出しが挿入されている場合、これは検出され、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 の専門家の助けをいただければ幸いです...