使用模拟和死亡测试
Using mocks and death tests
我发现 Google 测试的一个意想不到的行为,当涉及到死亡测试与对模拟对象的期望相结合时。
检查以下示例:
#include <gmock/gmock.h>
#include <cassert>
class Interface
{
public:
virtual void foo() = 0;
};
class InterfaceMock : public Interface
{
public:
MOCK_METHOD0(foo, void());
};
class Bar
{
public:
void call(Interface& interface)
{
interface.foo();
assert(false);
}
};
TEST(BarTest, call_fooGetsCalledAndDies)
{
InterfaceMock mock;
EXPECT_CALL(mock, foo());
ASSERT_DEATH({ Bar().call(mock); }, "");
}
测试失败,因为对 mock.foo()
的预期调用未能断言:
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from BarTest
[ RUN ] BarTest.call_fooGetsCalledAndDies
[WARNING] /tmp/tmp.sHHkM/gmock-1.7.0/gtest/src/gtest-death-test.cc:825:: Death tests use fork(), which is unsafe particularly in a threaded context. For this test, Google Test couldn't detect the number of threads.
foo.cc:31: Failure
Actual function call count doesn't match EXPECT_CALL(mock, foo())...
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] BarTest.call_fooGetsCalledAndDies (1 ms)
[----------] 1 test from BarTest (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (2 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] BarTest.call_fooGetsCalledAndDies
1 FAILED TEST
有趣的是,如果 EXPECT_CALL(mock, foo())
行被注释,Google Mock 会针对意外调用 mock.foo()
:
发出警告
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from BarTest
[ RUN ] BarTest.call_fooGetsCalledAndDies
[WARNING] /tmp/tmp.sHHkM/gmock-1.7.0/gtest/src/gtest-death-test.cc:825:: Death tests use fork(), which is unsafe particularly in a threaded context. For this test, Google Test couldn't detect the number of threads.
GMOCK WARNING:
Uninteresting mock function call - returning directly.
Function call: foo()
Stack trace:
[ OK ] BarTest.call_fooGetsCalledAndDies (1 ms)
[----------] 1 test from BarTest (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[ PASSED ] 1 test.
我想这在某种程度上与使用 fork()
和线程进行死亡测试的警告有关,但我无法将所有部分匹配在一起。
这是一个known limitation的死亡测试。内部 assert_death
分叉,因此在子进程中调用的模拟未在父进程中注册。如果您希望抑制警告,请考虑使用 NiceMock
.
如上正确回答,模拟调用未在子进程中注册。你必须在这个过程中写下你的 expect_calls 。以下应该有效:
TEST(BarTest, call_fooGetsCalledAndDies)
{
InterfaceMock mock;
ASSERT_DEATH({ [](){ EXPECT_CALL(mock, foo());
Bar().call(mock);} , "");
}
我发现 Google 测试的一个意想不到的行为,当涉及到死亡测试与对模拟对象的期望相结合时。
检查以下示例:
#include <gmock/gmock.h>
#include <cassert>
class Interface
{
public:
virtual void foo() = 0;
};
class InterfaceMock : public Interface
{
public:
MOCK_METHOD0(foo, void());
};
class Bar
{
public:
void call(Interface& interface)
{
interface.foo();
assert(false);
}
};
TEST(BarTest, call_fooGetsCalledAndDies)
{
InterfaceMock mock;
EXPECT_CALL(mock, foo());
ASSERT_DEATH({ Bar().call(mock); }, "");
}
测试失败,因为对 mock.foo()
的预期调用未能断言:
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from BarTest
[ RUN ] BarTest.call_fooGetsCalledAndDies
[WARNING] /tmp/tmp.sHHkM/gmock-1.7.0/gtest/src/gtest-death-test.cc:825:: Death tests use fork(), which is unsafe particularly in a threaded context. For this test, Google Test couldn't detect the number of threads.
foo.cc:31: Failure
Actual function call count doesn't match EXPECT_CALL(mock, foo())...
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] BarTest.call_fooGetsCalledAndDies (1 ms)
[----------] 1 test from BarTest (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (2 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] BarTest.call_fooGetsCalledAndDies
1 FAILED TEST
有趣的是,如果 EXPECT_CALL(mock, foo())
行被注释,Google Mock 会针对意外调用 mock.foo()
:
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from BarTest
[ RUN ] BarTest.call_fooGetsCalledAndDies
[WARNING] /tmp/tmp.sHHkM/gmock-1.7.0/gtest/src/gtest-death-test.cc:825:: Death tests use fork(), which is unsafe particularly in a threaded context. For this test, Google Test couldn't detect the number of threads.
GMOCK WARNING:
Uninteresting mock function call - returning directly.
Function call: foo()
Stack trace:
[ OK ] BarTest.call_fooGetsCalledAndDies (1 ms)
[----------] 1 test from BarTest (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[ PASSED ] 1 test.
我想这在某种程度上与使用 fork()
和线程进行死亡测试的警告有关,但我无法将所有部分匹配在一起。
这是一个known limitation的死亡测试。内部 assert_death
分叉,因此在子进程中调用的模拟未在父进程中注册。如果您希望抑制警告,请考虑使用 NiceMock
.
如上正确回答,模拟调用未在子进程中注册。你必须在这个过程中写下你的 expect_calls 。以下应该有效:
TEST(BarTest, call_fooGetsCalledAndDies)
{
InterfaceMock mock;
ASSERT_DEATH({ [](){ EXPECT_CALL(mock, foo());
Bar().call(mock);} , "");
}