我尝试模拟一个简单的 C++ 有什么问题吗? googlemock 的方法?

发布于 2024-11-03 12:49:43 字数 2023 浏览 1 评论 0原文

根据 C++ 单元测试模式方法进行标准库调用,我正在测试驱动网络抽象类的开发。为了对进行标准 C 库调用(我无法模拟)来处理 BSD 套接字的代码进行单元测试,我定义了一个接口 ISocket,我的实际实现 CSocket< /code> 和模拟 MockSocket 继承。

现在,我为 Network 类编写第一个单元测试,该类使用 ISocket 来完成繁重的工作:

#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

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

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&)

这是我的接口和模拟看起来像:

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

知道我在这里做错了什么吗?

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.

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁梦 2024-11-10 12:49:43

看起来您正在 TestBody 中的某处调用 MockSocket 的复制构造函数,而无需进行强制转换。

Looks like you're invoking the copy constructor for MockSocket somewhere in TestBody without a cast.

清眉祭 2024-11-10 12:49:43

我终于把它整理好了。让我困惑的是我想使用指针,但 googlemock 可以与类一起使用。这是有效的:

#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

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

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

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. ;)

凉风有信 2024-11-10 12:49:43

怎么样:

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

}

(免责声明 - 我没有检查它是否编译......)

How about:

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

}

(Disclaimer - I did not check if it compiles...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文