使用Googletest,我无法模拟系统功能 [英] with Googletest i am not able to mock system function

查看:64
本文介绍了使用Googletest,我无法模拟系统功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟系统有趣的ioctl和套接字,但它总是调用系统功能的原始定义.这是我使用gmock编写的gtest代码.

I am trying to mock the system fun ioctl and socket but it is always calling the original definition of system function. Here is the gtest code i have written using gmock.

请仔细阅读代码,帮助我找出问题所在.有没有一种方法可以在单元测试中使用gmock模拟系统功能.如果有的话,请提供给我一个模拟系统功能的示例代码.

Please go through the code and help me find what is wrong. Is there a way to mock the system functions using gmock in unit testing. If any please provide me with an example code that mocks system function.

在test.hpp文件中

In the test.hpp file

class SystemFun
{
    public:
        virtual ~SystemFun() {}
        virtual int ioctl(int inetSocket, int counters, struct ifreq *device) = 0;
        virtual int socket(int domain, int type, int protocol) = 0;
};

class SystemFunMock : public SystemFun
{
    private:
        static SystemFunMock *oSystemFunMockInstance;
        static bool instanceFlag;
    public:
        virtual ~SystemFunMock() { instanceFlag = false; }
        MOCK_METHOD3(ioctl, int(int inetSocket, int counters ,struct ifreq *device));
        MOCK_METHOD3(socket, int(int domain, int type, int protocol));
        static SystemFunMock *getInstance()
        {
            if (!instanceFlag)
            {
                oSystemFunMockInstance = new SystemFunMock();
                instanceFlag = true;
            }
            return oSystemFunMockInstance;
        }
};

In the test.cpp file the code
bool SystemFunMock::instanceFlag = false;
SystemFunMock* SystemFunMock::oSystemFunMockInstance = NULL;

int socket(int domain, int type, int protocol)
{
    SystemFunMock* oSystemFunMock = SystemFunMock::getInstance();
    return oSystemFunMock->socket(domain, type, protocol);
}
int ioctl(int inetSocket, int counters, struct ifreq *device)
{
    SystemFunMock* oSystemFunMock = SystemFunMock::getInstance();
    return oSystemFunMock->ioctl(inetSocket, counters, device);
}

TEST_F(EtherPortUT, linuxHalCommonTest)    
{
    SetUp();    
    SystemFunMock* oSystemFunMock = SystemFunMock::getInstance();    
    {    
        InSequence s;    
        EXPECT_CALL(*oSystemFunMock, ioctl(_,_,_))
            .WillOnce(Return(0))
            .WillOnce(Return(-1));
        EXPECT_CALL(*oSystemFunMock, socket(_,_,_))
            .WillOnce(Return(-1));
    }
.....
pLNXNetIface->getLinkSpeed( 356,  &speed ); // this fun def has 2 ioctl     calls but calling the original function.
pLNXNetIface->watchNetLink( iPort ); // this fun def has socket call.
...

}

Definition of the functions getLinkSpeed
bool LinuxNetworkInterface::getLinkSpeed( int sd, uint32_t *speed )
{
     struct ifreq ifr;
     struct ethtool_cmd edata;
     ifr.ifr_ifindex = ifindex;
     if( ioctl( sd, SIOCGIFNAME, &ifr ) == -1 )
     {
          GPTP_LOG_ERROR
               ( "%s: SIOCGIFNAME failed: %s", __PRETTY_FUNCTION__,
                 strerror( errno ));
          return false;
     }
     ifr.ifr_data = (char *) &edata;
     edata.cmd = ETHTOOL_GSET;
     if( ioctl( sd, SIOCETHTOOL, &ifr ) == -1 )
     {
          GPTP_LOG_ERROR
               ( "%s: SIOCETHTOOL failed: %s", __PRETTY_FUNCTION__,
                 strerror( errno ));
          return false;
     }
...
}

推荐答案

您不能模拟一个免费功能.您必须使用一个接口并使用该接口调用该函数:

You cannot mock a free function. You have to use an interface and call the function using the interface:

bool LinuxNetworkInterface::getLinkSpeed( int sd, uint32_t *speed )
{
     ...
     if(m_pSystemFun->ioctl( sd, SIOCGIFNAME, &ifr ) == -1 )

您必须为 LinuxNetworkInterface 提供某种方式来获取对 SystemFun 的指针或引用.在测试中,您将模拟类提供给 LinuxNetworkInterface .在您的实际应用程序中,您为 LinuxNetworkInterface 提供了一个实际的实现.

You must provide some way for LinuxNetworkInterface to get a pointer or reference to SystemFun. In your tests you provide the mock class to LinuxNetworkInterface. In your actual application you provide an actual implementation to LinuxNetworkInterface.

看来您在正确的轨道上.但是 SystemFun 接口不仅用于测试,而且还可以在应用程序中使用.

It seems you are on the right track. But the interface SystemFun is not just for testing but is also to be used in the application.

这篇关于使用Googletest,我无法模拟系统功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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