2013-04-11 238 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的操作系统能够编写核心文件以便稍后进行分析。