I have a testing class that instantiates a class that I wish to be mocked. Is this possible within the gmock framework? From what I read, it seems that the mocked object has to be created and passed to the object being tested and that is not what I want.
I've got it so that I can inject my mocked class into the source, but I'm stuck as to how to get it to generate the appropriate object.
The answer is to put the EXPECT_CALL()
into the constructor, using *this
as the first parameter. Of course, this only works if you expect it to be the same all the time.
This will get more complex if the expected behaviour is going to vary from instance to instance, unless each instance uses a different constructor.
If using the same constructor, then your mocked class could use a static counter which could change what is expected depending on the instance count.