我开始在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