GMock 死亡案例 - 模拟函数未被调用
GMock death case - mock function not being called
- 我在
my_inet.cpp
文件中创建了一个外部套接字的模拟 api。
- 该套接字的 GMock 函数 api 在
mock.h
文件中。
- 我正在使用我在
server.cpp
文件中创建的 my_inet
的套接字 api。
- 测试写在
gtest.cpp
.
这里写的死亡案例正在按照输出执行。输出表明该进程已终止。但它也表示从未进行过 socket()
调用,因此案例显示为失败。
请告知这是什么原因,解决方法是什么。
gtest.cpp
TEST(MyTest, SocketConnectionFail)
{
MockMyTCPAPI obj_myTCP;
EXPECT_CALL( obj_myTCP, socket( 1, 0, 0 ))
.Times( 1 )
.WillOnce( Return( -1 ));
Server obj_server( &obj_myTCP );
EXPECT_DEATH( obj_server.InitializeSocket(), "No socket connection!");
}
server.cpp
int Server::InitializeSocket()
{
if( ret_val_socket == -1 )
{
ret_val_socket = myTCPAPI->socket( PF_INET, SOCK_STREAM, 0 );
if( ret_val_socket == -1 )
{
printf( "\nNo socket connection!" );
exit(1);
}
return ret_val_socket;
}
else
{
printf( "Warning! Attempting to create socket again. %d" , ret_val_socket);
return 2;
}
return 0;
}
my_inet.cpp
int MyTCPAPI::socket( int arg1, int arg2, int arg3 )
{
return -1;
}
输出:
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MyTest
[ RUN ] MyTest.SocketConnectionFail
[WARNING] /usr/src/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.
No socket connection!
/home/../Documents/office/tdd/tcp/server/gtest.cpp:49: ERROR: this mock object (used in test MyTest.SocketConnectionFail) should be deleted but never is. Its address is @0x7fff2e94a890.
ERROR: 1 leaked mock object found at program exit.
/home/../Documents/office/tdd/tcp/server/gtest.cpp:56: Failure
Death test: obj_server.InitializeSocket()
Result: died but not with expected error.
Expected: No socket connection!
Actual msg:
[ DEATH ]
/home/../Documents/office/tdd/tcp/server/gtest.cpp:49: Failure
Actual function call count doesn't match EXPECT_CALL(obj_myTCP, socket( 1, 0, 0 ))...
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] MyTest.SocketConnectionFail (3 ms)
[----------] 1 test from MyTest (3 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (3 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] MyTest.SocketConnectionFail
1 FAILED TEST
根据 docs,EXPECT_DEATH
和 ASSERT_DEATH
生成一个执行死亡测试语句的子进程(在本例中为 obj_server.InitializeSocket()
)。
子进程终止后,他们检查退出代码和 stderr
消息。如果退出码为0
或stderr
中的消息与预期值不匹配,则测试无效。另一方面,stdout
未被选中。
因此,printf( "\nNo socket connection!" )
必须在应用程序退出之前用 fprintf(stderr, "\nNo socket connection!" )
替换:
int Server::InitializeSocket()
{
if( ret_val_socket == -1 )
{
ret_val_socket = myTCPAPI->socket( PF_INET, SOCK_STREAM, 0 );
if( ret_val_socket == -1 )
{
fprintf(stderr, "\nNo socket connection!" ); // print to stderr here
exit(1);
}
return ret_val_socket;
}
else
{
printf( "Warning! Attempting to create socket again. %d" , ret_val_socket);
return 2;
}
return 0;
}
- 我在
my_inet.cpp
文件中创建了一个外部套接字的模拟 api。 - 该套接字的 GMock 函数 api 在
mock.h
文件中。 - 我正在使用我在
server.cpp
文件中创建的my_inet
的套接字 api。 - 测试写在
gtest.cpp
.
这里写的死亡案例正在按照输出执行。输出表明该进程已终止。但它也表示从未进行过 socket()
调用,因此案例显示为失败。
请告知这是什么原因,解决方法是什么。
gtest.cpp
TEST(MyTest, SocketConnectionFail)
{
MockMyTCPAPI obj_myTCP;
EXPECT_CALL( obj_myTCP, socket( 1, 0, 0 ))
.Times( 1 )
.WillOnce( Return( -1 ));
Server obj_server( &obj_myTCP );
EXPECT_DEATH( obj_server.InitializeSocket(), "No socket connection!");
}
server.cpp
int Server::InitializeSocket()
{
if( ret_val_socket == -1 )
{
ret_val_socket = myTCPAPI->socket( PF_INET, SOCK_STREAM, 0 );
if( ret_val_socket == -1 )
{
printf( "\nNo socket connection!" );
exit(1);
}
return ret_val_socket;
}
else
{
printf( "Warning! Attempting to create socket again. %d" , ret_val_socket);
return 2;
}
return 0;
}
my_inet.cpp
int MyTCPAPI::socket( int arg1, int arg2, int arg3 )
{
return -1;
}
输出:
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MyTest
[ RUN ] MyTest.SocketConnectionFail
[WARNING] /usr/src/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.
No socket connection!
/home/../Documents/office/tdd/tcp/server/gtest.cpp:49: ERROR: this mock object (used in test MyTest.SocketConnectionFail) should be deleted but never is. Its address is @0x7fff2e94a890.
ERROR: 1 leaked mock object found at program exit.
/home/../Documents/office/tdd/tcp/server/gtest.cpp:56: Failure
Death test: obj_server.InitializeSocket()
Result: died but not with expected error.
Expected: No socket connection!
Actual msg:
[ DEATH ]
/home/../Documents/office/tdd/tcp/server/gtest.cpp:49: Failure
Actual function call count doesn't match EXPECT_CALL(obj_myTCP, socket( 1, 0, 0 ))...
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] MyTest.SocketConnectionFail (3 ms)
[----------] 1 test from MyTest (3 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (3 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] MyTest.SocketConnectionFail
1 FAILED TEST
根据 docs,EXPECT_DEATH
和 ASSERT_DEATH
生成一个执行死亡测试语句的子进程(在本例中为 obj_server.InitializeSocket()
)。
子进程终止后,他们检查退出代码和 stderr
消息。如果退出码为0
或stderr
中的消息与预期值不匹配,则测试无效。另一方面,stdout
未被选中。
因此,printf( "\nNo socket connection!" )
必须在应用程序退出之前用 fprintf(stderr, "\nNo socket connection!" )
替换:
int Server::InitializeSocket()
{
if( ret_val_socket == -1 )
{
ret_val_socket = myTCPAPI->socket( PF_INET, SOCK_STREAM, 0 );
if( ret_val_socket == -1 )
{
fprintf(stderr, "\nNo socket connection!" ); // print to stderr here
exit(1);
}
return ret_val_socket;
}
else
{
printf( "Warning! Attempting to create socket again. %d" , ret_val_socket);
return 2;
}
return 0;
}