2013-04-11 240 views
6

我開始在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)); 
} 

猜測我在這裏犯了一個非常基本的錯誤,任何人都可以看到我出錯的地方嗎? 謝謝!

[原創編輯,以使代碼&控制檯輸出匹配]

+0

這是生成該輸出的實際代碼嗎?我看不到'ProductionTest.CallTheUseOnProductionUser()'和兩個'ProductionTest.CallTheMethodUnderTest()'。 – metal 2013-04-11 15:54:38

+0

@metal它在那裏。 'TEST(ProductionTest,CallTheMethodUnderTest)'是一個宏,指定自動被稱爲購買測試框架的單元測試。 – 2013-04-11 15:57:18

+0

我看到測試列出了兩次,並且沒有'CallTheUseOnProductionUser'的符號。誠然,我不熟悉GoogleMock,但我已經使用了其他幾個測試框架。我錯過了什麼嗎? – metal 2013-04-11 15:59:52

回答

3

我遇到了同樣的問題,當我編譯gmock爲DLL而在另一個項目中鏈接它。 經過很多嘗試,我發現原因是:

你必須在相同的配置編譯gmock和你的項目!

這意味着如果您想在DEBUG(RELEASE)模式下鏈接它,您必須在DEBUG(RELEASE)配置中編譯gmock。否則,

未知文件:錯誤:SEH異常與代碼0xc0000005在測試正文中拋出。

總是發生。

我希望我的經驗可以幫助你,儘管你可能在不同的場景中遇到這個問題。

+1

我看到了類似的問題,雖然它與GTEST是特別。我的項目建立,鏈接,並運行在Release模式只是罰款,但不是在調試模式。我不知道是什麼問題,雖然我已經在這兩種模式下建立GTEST和CMake的依賴於我的生成類型查找相應的GTEST庫。任何更多的評論將極大地幫助。 – 2015-01-28 20:44:23

+0

,幫助我出。 – 2015-10-28 06:45:41

2

我想你可能會迫使GTEST千萬不要cloack確切異常(什麼可能使用像

::testing::GTEST_FLAG(catch_exceptions) = false; 

,或者在命令行代碼來完成) 如果然後使用一個調試器,你」我會很容易地獲得堆棧。或者,即使你不這樣做,我期望類似nix的操作系統能夠編寫核心文件以便稍後進行分析。