Mocking data members

25 views
Skip to first unread message

Thomas W.

unread,
Jun 10, 2019, 2:03:09 PM6/10/19
to googletes...@googlegroups.com
In "A Case for Mock Turtles" is a pretty simple example of defining an interface for your dependency.  But what if my class has data members of my dependency? You can't declare data members of an abstract class. 

I can make my class a template and resolve the data member on instantiation of the class. So during tests I can instantiate with the mock class, and during production I can instantiate with the real class. I can use the "real" class as a default template parameter so it doesn't muck up my API.

But this seems to mean I have to use a lot of dynamic casting on my data members if I want code completion.  And all of this feels funny to me.  Am I going down the right path here?

Thomas W.

unread,
Jun 11, 2019, 9:37:44 AM6/11/19
to Google C++ Testing Framework
Instead of forcing my dependencies to be virtual classes - I build the project twice. For test I like with mocks and for production I link to the actual dependencies.  This seems way easier and is less invasive to forcing changes on production code.
Reply all
Reply to author
Forward
0 new messages