How to mock a class using virtual and non-virtual methods using Google Mock?

I have a class that I want to mock using Google Mock. My class has BOTH non-virtual and virtual methods. I read the Google Mock ForDummies and the Google Mock CookBook . The examples and explanations provided by these resources mention classes with ALL virtual functions or NO virtual functions, but not one with both. So, I have two questions:

(1) Is it possible to mock a class with mixed virtual / non-virtual types?

(2) What method should be used (if question 1 is correct) to mock this class, (If question 1 is incorrect), what can be used instead?

A bit of code if this helps:

class Time_Device : public Time_Device_Interface
{   
private:
     ...
     bool read32_irig_data( uint32_t *data_read, uint32_t reg_address);
     bool thread_monitor_irig_changed( irig_callback_t callback );
public:
     ...
     virtual bool set_time( struct time_sample const &time );
     virtual bool get_time( struct time_sample *time );
     virtual bool register_is_connected_notification( 
         irig_callback_t callback );
 };

Tiny background:

Google Mock Google Test, , .. Google Test, .

Visual Studio 2010, CMake

Google Test Google Mock

.

+7
2

(1) / ?

, , . .

:

struct Time_Device_Mock : public Time_Device_Interface
{
    MOCK_CONST_METHOD1( set_time, bool(time_sample const &) );
    MOCK_CONST_METHOD1( get_time, bool(time_sample *) );
    MOCK_CONST_METHOD1( register_is_connected_notification, bool(irig_callback_t) );
};

(2) ( 1 ), ( 1 ), ?

. , , . .

, , . , .

+5

, . , -, , . , , , TestedClass:: memfunc (T &).

class TestedClass
{
public:
   template<typename T>
   void memfunc(T& obj);
};

class OriginalObject {...};
class MockObject {...};

in the test-function:
TestedClass obj;
MockObject  mockObj;
obj.memfun(mockObj);

- , -.

0

All Articles