我開始在googletest中使用googlemock,但得到一個SEH異常,我無法弄清楚。使用googlemock時SEH異常
的錯誤信息是:
unknown file: error: SEH exception with code 0xc0000005 thrown in the test body.
我已經閱讀SO和其他一些類似的問題,但我還沒有找到一個答案對於這樣一個簡單的例子。
即這發生在我的真實代碼上,但我也在下面的簡單示例中重現了錯誤。我正在用MSVC2008構建。
代碼重現錯誤:
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from ProductionTest
[ RUN ] ProductionTest.CallTheProductionFunction
CALLED ProductionCode::fn
[ OK ] ProductionTest.CallTheProductionFunction (4 ms)
[ RUN ] ProductionTest.CallTheMethodUnderTest
CALLED ProductionCode::fn
[ OK ] ProductionTest.CallTheMethodUnderTest (2 ms)
[ RUN ] ProductionTest.CallTheMethodUnderTestWithMock
unknown file: error: SEH exception with code 0xc0000005 thrown in the test body.
[ FAILED ] ProductionTest.CallTheMethodUnderTestWithMock (0 ms)
[----------] 3 tests from ProductionTest (10 ms total)
[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (13 ms total)
[ PASSED ] 2 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] ProductionTest.CallTheMethodUnderTestWithMock
1 FAILED TEST
.\simple.cpp(59): ERROR: this mock object (used in test ProductionTest.CallTheMe
thodUnderTestWithMock) should be deleted but never is. Its address is @000000000
014F800.
ERROR: 1 leaked mock object found at program exit.
Press any key to continue . . .
我用我自己的主要功能如下:
#include "gtest/gtest.h"
#include "gmock/gmock.h"
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();
}
我從控制檯我的測試輸出
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <iostream>
using testing::Exactly;
class Production
{
public:
virtual ~Production() {};
virtual void fn() = 0;
};
class ProductionCode : public Production
{
public:
virtual ~ProductionCode() {};
void fn()
{
std::cout << "CALLED ProductionCode::fn" << std::endl;
}
};
class MockProduction : public Production
{
public:
virtual ~MockProduction() {};
MOCK_METHOD0(fn, void());
};
class ProductionUser
{
public:
void methodUnderTest(Production *p)
{
p->fn();
}
};
TEST(ProductionTest, CallTheProductionFunction) {
ProductionCode p;
ASSERT_NO_THROW(p.fn());
}
TEST(ProductionTest, CallTheMethodUnderTest) {
Production* p = new ProductionCode;
ProductionUser u;
ASSERT_NO_THROW(u.methodUnderTest(p));
delete p;
}
TEST(ProductionTest, CallTheMethodUnderTestWithMock) {
MockProduction m;
EXPECT_CALL(m, fn())
.Times(Exactly(1));
ProductionUser u;
ASSERT_NO_THROW(u.methodUnderTest(&m));
}
猜測我在這裏犯了一個非常基本的錯誤,任何人都可以看到我出錯的地方嗎? 謝謝!
[原創編輯,以使代碼&控制檯輸出匹配]
這是生成該輸出的實際代碼嗎?我看不到'ProductionTest.CallTheUseOnProductionUser()'和兩個'ProductionTest.CallTheMethodUnderTest()'。 – metal 2013-04-11 15:54:38
@metal它在那裏。 'TEST(ProductionTest,CallTheMethodUnderTest)'是一個宏,指定自動被稱爲購買測試框架的單元測試。 – 2013-04-11 15:57:18
我看到測試列出了兩次,並且沒有'CallTheUseOnProductionUser'的符號。誠然,我不熟悉GoogleMock,但我已經使用了其他幾個測試框架。我錯過了什麼嗎? – metal 2013-04-11 15:59:52