c – 使用googlemock时的SEH异常

前端之家收集整理的这篇文章主要介绍了c – 使用googlemock时的SEH异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我开始使用googlemock与googletest,但我得到了一个我无法弄清楚的SEH例外.

错误消息是:

unknown file: error: SEH exception with code 0xc0000005 thrown in the test body.

我已经在SO和其他地方读过一些类似的问题,但我还没有找到这样一个简单例子的答案.

即这是在我的真实代码上发生的,但我也在下面这个非常简单的例子中重现了错误.我正在使用MSVC2008构建.

重现错误代码

#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));
}

我从控制台输出的测试结果:

[==========] 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();
}

我猜我在这里犯了一个非常基本的错误,谁能看到我哪里出错?
谢谢!

[原创编辑制作代码&控制台输出匹配]

解决方法

当我将gmock编译为DLL并将其链接到另一个项目时,我遇到了同样的问题.
经过大量的尝试,我发现原因是:

你必须在相同的配置中编译gmock和你的项目!

这意味着如果要在DEBUG(RELEASE)模式下链接它,则必须在DEBUG(RELEASE)配置中编译gmock.如果没有,那么

未知文件错误:测试正文中抛出代码为0xc0000005的SEH异常.

总是发生.

我希望我的经验可以帮助你,尽管你可能会在不同的场景中遇到这个问题.

猜你在找的C&C++相关文章