Question regarding abstract interfaces and mocks

167 views
Skip to first unread message

Cindy

unread,
Nov 7, 2016, 10:32:16 AM11/7/16
to Google C++ Mocking Framework
I apologize if this is a stupid question, but I'm new to C++, GTest, and GMock and am trying my best to chug along and learn it all.

Essentially, I have a class I'll call Foo. Within it, Foo has a shared pointer to BarData, and within BarData, I have a Bar object. Equivalently:

class Foo{

std::shared_ptr<BarData> data;

}

and 

class BarData{

Bar bar;

}

I am creating a mock for Bar because at the moment, I am testing Foo. From what I understand, GMock recommends creating a pure virtual interface to Bar (as the thing that I am mocking) so that whenever I implement Bar, it is guaranteed to share the same interface as my mock.

Following their example, I can create a class MockBar that inherits from Bar. But.... this is problematic. My BarData class contains an object of type Bar. Functions that I've already implemented take as input argument an object of type Bar. I could potentially change the name of all of those to point to a MockBar, but that defeats the point of a mock in that it should have the same behavior- I shouldn't need to change the name of everything to a MockBar. I can't have it take in a Bar, as that's a purely abstract class- I can't make an object that would take it in.

Even if I then decided to make BarData have a shared_ptr to that class, that still requires me to change all of the functions I have so far that take references to type Bar, and that goes against the spec that I'm working with for these functions (they should conform to take an object of type ___ in them)

I'm sure I'm missing something obvious, but any recommendations what to do to get around this?
Reply all
Reply to author
Forward
0 new messages