を実施したい場合にネックとなるのが、他のクラスなどへの依存性ですが、これを
Googlemockを使用してモックオブジェクトを差し込んで、テストの実施およびテストの
網羅度を向上させることができます。
下記のように同じ名前のメンバ関数を持つようなクラスのモック化ではまってしまったので
メモしておきます。
class SomeClass
{
public:
...
virtual void someMethod(int i);
virtual void someMethod(std::string& str);
...
};
このようなクラスをMock化して使用する場合には、EXPECT_CALL()マクロ使用において、
Matcherの記述において引数の型を明示する必要があるため ::testing::Matcher<> というの
を記載する必要があります。
class MockSomeClass : public SomeClass
{
public:
...
MOCK_METHOD1(someMethod, void(int));
MOCK_METHOD1(someMethod, void(std::string&));
};
....
// Test code
MockSomeClass mock;
...
// In case that we would expect latter method is invoked here.
EXPECT_CALL(mock, someMethod(::testing::Matcher<std::string&>(::testing::_)))
...
// instead of
// EXPECT_CALL(mock, someMethod, ::testing::_);
// ...
Are you using Googlemock? Although I've just started playing with it, I found it is
quite useful. Whenever trying to perform unit testing, what can be some obstacle to
achieve unit testing would be dependency on other classes etc.. However, we will be
able to eliminate such dependency to outside by introducing mock object instead of
the actual class object. This is the way that we can achieve unit testing and even
improve test coverage toward developed software. The other day I had some hard
time to handle the following case for a while in handling some polymorphic class
where the same name of member methods are defined.
class SomeClass
{
public:
...
virtual void someMethod(int i);
virtual void someMethod(std::string& str);
...
};
In case of trying to mock such class, we would need to have a bit special treatment
when using EXPECT_CALL() macro. When you write down matcher portion, you are
supposed to use ::testing::Matcher<> to specify the argument type which is being
expected here. Otherwise, EXPECT_CALL() can be treated as ambiguous...
class MockSomeClass : public SomeClass
{
public:
...
MOCK_METHOD1(someMethod, void(int));
MOCK_METHOD1(someMethod, void(std::string&));
};
....
// Test code
MockSomeClass mock;
...
// In case that we would expect latter method is invoked here.
EXPECT_CALL(mock, someMethod(::testing::Matcher<std::string&>(::testing::_)))
...
// instead of
// EXPECT_CALL(mock, someMethod, ::testing::_);
// ...
0 件のコメント:
コメントを投稿