我试图用googlemock模拟一个简单的C ++方法有什么问题? [英] What is wrong with my attempts to mock a simple C++ method with googlemock?

查看:328
本文介绍了我试图用googlemock模拟一个简单的C ++方法有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据单元格模式测试一个C ++方法,使标准库调用,我测试驱动网络抽象类的开发。为了单元测试代码,使标准的C库调用(我不能mock)处理BSD套接字,我定义了一个接口 ISocket ,从我的真实实现 CSocket 和mock MockSocket 继承。

As per Patterns for unit testing a C++ method that makes a standard library call, I'm test-driving development of a network-abstracting class. In order to unit test code that makes standard C library calls (which I can't mock) to handle BSD sockets, I've defined an interface ISocket from which both my real implementation CSocket and mock MockSocket inherit.

写我的第一个单元测试为网络类,使用 ISocket 为重型起重:

Now I write my first unit test for the Network class, which uses an ISocket for the heavy lifting:

#include "gmock/gmock.h"
#include "gtest/gtest.h"
// C standard library includes omitted
#include "MockSocket.h"
#include "Network.h"

using ::testing::Return;
namespace JrStream {
  class NetworkTest : public ::testing::Test {
  protected:
    Network net;
    ISocket * socket_ptr;

    virtual void SetUp() {
      socket_ptr = new MockSocket();
    }
  };

  TEST_F(NetworkTest, InitCallsSocket) {
    EXPECT_CALL((MockSocket)*socket_ptr, Socket(AF_INET, SOCK_STREAM, 0))
        .Times(1)
        .WillOnce(Return(5)); //fake file descriptor

    ASSERT_TRUE(net.init(socket_ptr));
  }
} // namespace
// gtest boilerplate main() omitted


$ b b

但是我在编译时遇到这个错误:

But I get this error on compilation:

g++ -g -Wall -Lgtest-1.5.0/lib -Igmock-1.5.0/include -Igtest-1.5.0/include -I.. -I../../Debug/src -c -o NetworkTest.o NetworkTest.cc
NetworkTest.cc: In member function ‘virtual void JrStream::NetworkTest_InitCallsSocket_Test::TestBody()’:
NetworkTest.cc:35: error: no matching function for call to ‘JrStream::MockSocket::MockSocket(JrStream::ISocket&)’
MockSocket.h:16: note: candidates are: JrStream::MockSocket::MockSocket()
MockSocket.h:16: note:                 JrStream::MockSocket::MockSocket(const JrStream::MockSocket&)

这里是我的界面和模拟看起来像:

And here is what my interface and mock look like:

namespace JrStream {
  class ISocket {
    virtual int Socket(int domain, int type, int protocol) = 0;
  };
}

#include "gmock/gmock.h"
#include "ISocket.h"
namespace JrStream {
  class MockSocket: public JrStream::ISocket {
    MOCK_METHOD3(Socket, int(int domain, int type, int protocol));
  };
}

任何想法我在这里做错了什么?

Any ideas what I'm doing wrong here?

推荐答案

我终于得到排序。引发我的是我想使用一个指针,但googlemock使用类。这是什么工作:

I finally got it sorted. What was tripping me up is that I wanted to use a pointer, but googlemock works with classes. Here's what works:

#include "gmock/gmock.h"
#include "gtest/gtest.h"
// C standard library includes omitted
#include "MockSocket.h"
#include "Network.h"

using ::testing::Return;
namespace JrStream {
  class NetworkTest : public ::testing::Test {
  protected:
    Network net;
    MockSocket sock;
  };

  TEST_F(NetworkTest, InitCallsSocket) {
    EXPECT_CALL(sock, Socket(AF_INET, SOCK_STREAM, 0))
        .Times(1)
        .WillOnce(Return(5)); //fake file descriptor

    ASSERT_TRUE(net.init(&sock));
  }
} // namespace
// gtest boilerplate main() omitted


$ b b

这是很好的,但我想知道如果我真的需要我的指针,如何使这项工作。听起来像是读者的练习。 ;)

This is well and good, but I wonder how to make this work if I really needed my pointers. Sounds like an exercise for the reader. ;)

这篇关于我试图用googlemock模拟一个简单的C ++方法有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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