Is there any way to access a Mocked object created from some other factory function.

Skip to first unread message

Alex Shaver

Nov 15, 2016, 4:45:52 PM11/15/16
to Google C++ Mocking Framework
Suppose I have
class Foo{
Foo():mBar{someFactory::createBar(std::string type)}{}

void doStuff(){mBar->stuff();}

::unique_ptr<Bar> mBar;

class Bar{
virtual void stuff(){}

class MockBar : public Bar{
MOCK_METHOD0(stuff, void());

Foo foo;

_CALL(???, stuff());


Where someFactory is a factory defined elsewhere that returns subclasses of bar depending on 'type.' Suppose I've managed to get that factory to return a MockBar. Is there any way to get that mock object's address to be able to set expectations? 

Samuel Benzaquen

Nov 15, 2016, 5:59:53 PM11/15/16
to Alex Shaver, Google C++ Mocking Framework
If you know it is a MockBar, you can just downcast with static_cast<MockBar*>(foo.mBar.get()) and use that pointer.
You might also be reaching a point where mocks don't help you anymore. Maybe a fake can solve your problem?


You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
For more options, visit

Alex Shaver

Nov 15, 2016, 6:26:55 PM11/15/16
to Google C++ Mocking Framework,
mBar is private, so I can't just access it like that. I think you may be right that this is a case where there's not much I can do from the google mock suite, but I just wanted to be sure in case someone could think of something (say, for instance, the mock objects register themselves to the test framework somehow that I could access via something like currentTest().getMockedObjects<MockBar>(). (not that any of these exist, but that's the kind of thought)
To unsubscribe from this group and stop receiving emails from it, send an email to
Reply all
Reply to author
0 new messages