Vikram Ranabhatt
Vikram Ranabhatt

Reputation: 7620

Unit Test for Callback using GTest

This is the Class design for Device Discovery Library in the network using Bonjour.I need to develop Test case for it using GTest.I am new to GTEst.

  1. Client Program need to implement IDeviceEnumerationCallback to receive Device Information

  2. Callback will be called after Interval time and frequency Say Interval is 200 ms and frequency is 2. it will call the two times callback after 200 ms.

    class IDeviceEnumerationCallback
    {
    public:
    /* This callback is called when Device are Enumerated and is regsitered in EnumerateWiFiDevice method */
    
      virtual void onDeviceDiscovered( DeviceInfo* pDeviceInfo,unsigned short nNoOfDevice,void* pContext) = 0;  
    };
    
    IDeviceDiscovery
    {
       virtual int InitialiseDeviceDiscovery(IDeviceEnumerationCallback*) = 0;
       virtual void UnInitialiseDeviceDiscovery() = 0;  
       virtual int  EnumerateDevice() = 0;
       virtual void SetDiscoveryInterval(unsigned long nDiscoveryInterval);
       virtual void SetDiscoveryFrequency(unsigned short nFrequency);
       virtual unsigned long GettDiscoveryInterval();
       virtual unsigned short GettDiscoveryFrequency(); 
    
    
    }
    
    class CDeviceDiscovery : public IDeviceDiscovery
    {
     // implemenation
     }
    

When I Develop Unit Test for EnumerateDevice() It will return immediately Saying -1 or 1.But the result will be returned in the callback.How to Know Whether Device is enumerated properly or not using GTest. Do I require GTest Mock Here??

Upvotes: 0

Views: 7161

Answers (1)

jzwiener
jzwiener

Reputation: 8452

You could use Gmock for this. A good explanation can be found on this page: http://code.google.com/p/googlemock/wiki/ForDummies

You would mock IDeviceEnumerationCallback

#include <gmock/gmock.h>
class MockIDeviceEnumerationCallback : public IDeviceEnumerationCallback
{
public:
    MOCK_METHOD3(onDeviceDiscovered, void(DeviceInfo* pDeviceInfo,unsigned short nNoOfDevice,void* pContext));
};

and expect an call to the function using

MockIDeviceEnumerationCallback mock;
EXPECT_CALL(mock, onDeviceDiscovered(_, _, _))
    .WillOnce(Return(1));

Upvotes: 1

Related Questions