2

I have tested Google Mock for virtual function, but am unable to do it for non-virtual. I didn't understand the concept of "hi-perf dependency injection".

Below is class having one non-virtual function. I want to use Google Mock to test this function. For simplicity, I have removed core part and just kept single return statement.

Below is example:

// gtest and gmock files are added class DetectorGPIO { DetectorGPIO(void); virtual ~DetectorGPIO() {} StartPulseHigh(); }; BOOL DetectorGPIO::StartPulseHigh() { return 1; } class MockDetectorGPIO : public DetectorGPIO { public: MOCK_METHOD0(StartPulseHigh,BOOL(void)); }; 

Am I missing any concept?

I am new to Google Mock. Any help will be appreciated.

3
  • As far as I have understood, it only mocks virtual polymorphic objects, which has always limited its usefulness to me. Commented Aug 11, 2017 at 6:47
  • What exactly is the problem? Do you have issues compiling? If so, what is the compiler output? I'm not familiar with Google Mock myself, but looking at your source code: You are using StartPulseHigh, but it doesn't seem to be defined in your DetectorGPIO class. Commented Aug 11, 2017 at 6:48
  • @Freakyy, Actually I forgot to add definition in class. i have added now. Commented Aug 11, 2017 at 7:35

1 Answer 1

1

You can read about hi-perf dependency injection in Google Mock Cookbook. I think it clearly describes proper way of using this concept.

In this type of mocking your MockDetectorGPIO does not need to inherit from DetectorGPIO. In place where you want to use concrete class (or mock) you should use templates:

template <class Detector> void ConfigureDetectorGPIO(Detector* pDetector) { pDetector->StartPulseHigh(); } 

Note that in Google Mock you are not obligated to mock existing methods.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.