정적 메서드를 노출하는 특성 템플릿 매개 변수를 사용하는 클래스를 단위 테스트 할 수있는 Visual Studio 2008 C++ 03 프로젝트가 있습니다 (정책 기반 디자인 , 전략 패턴). Google Test 및 Google Mock 프레임 워크를 사용하고 있습니다. 예를 들어"특성"템플릿 매개 변수를 사용하는 개체 테스트
:
/// the class under test
template< typename FooTraits >
class Foo
{
public:
void DoSomething()
{
FooTraits::handle_type h = FooTraits::Open("Foo");
/* ... */
FooTraits::Close(h);
};
};
/// a typical traits structure
struct SomeTraits
{
typedef HANDLE handle_type;
static handle_type Open(const char* name) { /* ... */ };
static void Close(handle_type h) { /* ... */ };
};
/// mocked traits that I would like to use for testing
struct MockTraits
{
typedef int handle_type;
static MOCK_METHOD1(Open, handle_type(const char*));
static MOCK_METHOD1(Close, void(handle_type));
};
/// the test function
TEST(FooTest, VerifyDoSomethingWorks)
{
Foo<MockTraits> foo_under_test;
// expect MockTraits::Open is called once
// expect MockTraits::Close is called once with the parameter returned from Open
foo_under_test.DoSomething();
};
는 분명히 이것은있는 그대로 작동하지 않습니다. Google 모의에서는 정적 메소드를 모의 할 수 없으므로 테스트에서 Mocked 클래스의 인스턴스를 만들어 동작 및 기대치를 설정해야합니다.
Google Test/Google Mock을 사용하여 템플릿 정책을 수락하는 클래스를 단위 테스트하는 올바른 방법은 무엇입니까?
천재입니다. 조롱 된 싱글 톤 클래스를 만든 다음 스텁 참조를 가져야합니다. 답변에 넣을 수 있습니까? 작은 녹색 체크 표시를 클릭하고 싶습니다. – PaulH