0
0

Google Mock の EXPECT_CALL 定義順について

Posted at

0.はじめに

Google Mock ドキュメント日本語訳(複数の Expectation を利用する)

Google Mock のデフォルトでは,モックメソッドが呼び出されると,引数にマッチするアクティブな Expectation が見つかるまで,定義順とは逆方向に Expectation を探索します

1.簡単なテストコードで動作を確認

テスト対象とモック
// モック
class MockMyCalck
{
public:
	MOCK_METHOD2(add, int(int, int));
	MOCK_METHOD2(sub, int(int, int));
} *mockPtr = NULL;

int add(int a, int b)
{
	EXPECT_NE(mockPtr, (MockMyCalck*)NULL);
	return mockPtr->add(a, b);
}

int sub(int a, int b)
{
	EXPECT_NE(mockPtr, (MockMyCalck*)NULL);
	return mockPtr->sub(a, b);
}

// テスト対象関数
void function(void)
{
	add(1, 2);
	add(3, 4);
	add(5, 6);
}
テスト対象関数と同じ順番に定義
TEST(Sample, 00)
{
	MockMyCalck mock;
	mockPtr = &mock;

	// Define Expection
	EXPECT_CALL(*mockPtr, add(1, 2)).Times(1);
	EXPECT_CALL(*mockPtr, add(3, 4)).Times(1);
	EXPECT_CALL(*mockPtr, add(5, 6)).Times(1);
	
	function();
}
適当な順番に定義
	// Define Expection
	EXPECT_CALL(*mockPtr, add(5, 6)).Times(1);
	EXPECT_CALL(*mockPtr, add(3, 4)).Times(1);
	EXPECT_CALL(*mockPtr, add(1, 2)).Times(1);
ループで定義
	// Define Expection
	std::vector<std::vector<int>> v = {{1, 2}, {3, 4}, {5, 6}};
	for(auto x : v){
		EXPECT_CALL(*mock_ptr, add(x.at(0), x.at(1))).Times(1);
	}
実行結果
[==========] Running 3 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 3 tests from Sample
[ RUN      ] Sample.00
[       OK ] Sample.00 (0 ms)
[ RUN      ] Sample.01
[       OK ] Sample.01 (0 ms)
[ RUN      ] Sample.02
[       OK ] Sample.02 (0 ms)
[----------] 3 tests from Sample (0 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 1 test suite ran. (0 ms total)
[  PASSED  ] 3 tests.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0