2

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 の専門家の助けをいただければ幸いです...

4

3 に答える 3

1

NeverCall はアサーションの例外をスローするように設計されているため、これを行うことはできません。私には完全に非論理的に思えます。

本当にそれを回避したい場合は、次のように設定してください

mocks.OnCallFunc(someFunc).Do(someFuncCalledHandler);

独自のハンドラーで、必要なロジックを実装できます。

bool callAllowed = true; //global variable
void someFuncCalledHandler()
{
    if (!callAllowed)
    {
        throw MyNeverCallException();
    }
}

テストでは、someFuncCalledHandler() の動作を操作できます。

callAllowed = false;
someFunc();

ところで、サンプルコードで行ったように、テストで配置とアクションコードを混同するのは悪い考えです

于 2016-07-29T08:28:12.290 に答える