GMock死亡案例-未调用模拟函数 [英] GMock death case - mock function not being called

查看:453
本文介绍了GMock死亡案例-未调用模拟函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我在my_inet.cpp文件中创建了一个外部套接字api的模拟.
  2. 该套接字api的GMock函数位于mock.h文件中.
  3. 我正在使用在server.cpp文件中创建的my_inet套接字API.
  4. 测试用gtest.cpp编写.
  1. I have created a mock of an external socket api in my_inet.cpp file.
  2. GMock functions for that socket api is in mock.h file.
  3. I am using my created socket api of my_inet in server.cpp file.
  4. The test is written in gtest.cpp.

此处写入的死亡案例正在根据输出执行.输出表明该过程已终止.但是它也说socket()调用从未进行过,因此该案例显示为失败.

The death case that is written here is executing as per the output. Output says that the the process died. But it also says that the socket() call was never made so the case is shown as failed.

请说明原因是什么,解决方案是什么.

Please tell what is the reason for this and what is the solution.

gtest.cpp

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

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

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

推荐答案

根据

According to the docs, EXPECT_DEATH and ASSERT_DEATH spawn a child process which executes the death test statement (in this case obj_server.InitializeSocket()).

子进程终止后,他们检查退出代码和stderr消息.如果退出代码为0stderr中的消息与预期值不匹配,则测试无效.另一方面,未检查stdout.

After termination of the child process, they check the exit code and the stderr message. If the exit code is 0 or the message in stderr does not match the expected value, the test is invalid. stdout on the other hand is not checked.

因此,必须在应用程序退出之前将printf( "\nNo socket connection!" )替换为fprintf(stderr, "\nNo socket connection!" ):

Therefore, printf( "\nNo socket connection!" ) has to be replaced with fprintf(stderr, "\nNo socket connection!" ) right before the applicatons exits:

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

这篇关于GMock死亡案例-未调用模拟函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆