GMock 프레임 워크를 처음 사용했습니다. 하지만, 다음 생산 어플리케이션 productionApp
및 테스트 응용 프로그램 testApp
이 있습니다. 내 제작 앱이 완벽하게 작동합니다. 그러나 조명기에서 첫 번째 테스트를 실행 한 후 테스트가 중단됩니다.GMOCK 테스트 픽스쳐가 Windows에서 깨집니다.
class IRegEditor
{
public:
virtual bool Read(int&) = 0;
virtual bool Write(const int&) = 0;
virtual ~IRegEditor() {}
};
class RegEditorImpl : public IRegEditor
{
public:
//use windows registry APIs instead
//read returns values based on current time.
//write fails for odd values.
bool Read(int& i) { if (system_clock::now().time_since_epoch().count() % 2)
return false; else { i = 10; return true; } }
bool Write(const int& j) { if (j % 2) return false; else return true; }
};
class RegEditorMock : public IRegEditor
{
public:
MOCK_METHOD1(Read, bool(int&));
MOCK_METHOD1(Write, bool(const int&));
};
class RegEditTest : public ::testing::Test
{
protected:
virtual void SetUp() {
regEditor.reset(®Mock);
}
std::shared_ptr<IRegEditor> regEditor;
RegEditorMock regMock;
};
class App
{
std::shared_ptr<IRegEditor> regEdit;
public:
//ctor to use in production
App() :regEdit{ std::make_shared<RegEditorImpl>() }
{}
//overloaded ctor to use for unit tests
App(std::shared_ptr<IRegEditor> regEditor) : regEdit{ regEditor }
{}
bool Writer(const int& number)
{
if (regEdit->Write(number))
{ std::cout << "write passed" << std::endl; return true; }
else
{ std::cout << "write failed" << std::endl; return false; }
}
bool Reader(int& number)
{
if (regEdit->Read(number))
{ std::cout << "read passed" << std::endl; return true; }
else { std::cout << "read failed" << std::endl; return false; }
}
};
TEST_F(RegEditTest, writeFails)
{
int number = 1;
EXPECT_CALL(regMock, Write(number)).Times(1).WillOnce(Return(false));
App testApp(regEditor);
EXPECT_FALSE(testApp.Writer(number));
}
TEST_F(RegEditTest, writeSucceeds)
{
int number = 2;
EXPECT_CALL(regMock, Write(number)).Times(1).WillOnce(Return(true));
App testApp(regEditor);
EXPECT_FALSE(testApp.Writer(number));
}
int main(int argc, char** argv) {
// The following line must be executed to initialize Google Mock
// (and Google Test) before running the tests.
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
테스트를 실행하는 중에 다음과 같은 오류가 발생합니다. GMock 라이브러리의 컴파일러 설정 호환성과 관련이 있습니까?
Unhandled exception at 0x77639D71 (ntdll.dll)
A heap has been corrupted (parameters: 0x7766D8D0).
그런 다음 wntdll.pdb에서 기호를로드 할 수 없다는 것을 보여줍니다. 메모리 -의 자동 저장 기간, 플러스 공유 포인터 regEditor
:
다음 생산 응용 프로그램은
int main()
{
App productionApp;
int num = 9;
productionApp.Reader(num);
productionApp.Writer(num);
std::cin.get();
return 0;
}