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

189 views
Skip to first unread message

Alex Shaver

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

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

private:
  std
::unique_ptr<Bar> mBar;
};

class Bar{
public:
 
virtual void stuff(){}
};

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

TEST
(Foo,DoStuff){
 
Foo foo;

  EXPECT
_CALL(???, stuff());

  foo
.doStuff();
}

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

unread,
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 googlemock+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googlemock/cb6ee8f6-2bc2-4f06-8ff1-b1746cafbed7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex Shaver

unread,
Nov 15, 2016, 6:26:55 PM11/15/16
to Google C++ Mocking Framework, alexs...@gmail.com
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 googlemock+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages