Can you use googlemock to set expectations on function that does not belong to a class?

StackOverflow https://stackoverflow.com/questions/10206513

  •  01-06-2021
  •  | 
  •  

Question

If in my main I have a function, func, that is called from main, can I use googlemock to say EXPECT_CALL(no_class_here, func).Times(1)?

no_class_here would be something like the scope operator to indicate that it doesn't belong to a class, ::. I don't know how that would work, but in the function implementation

I don't want to put func inside of a class when it is just a static function and does not require a class in order to run...

Should I just give up and put everything I'm going to test in a class all the time? I know that JUnit and JMock, everything is a class in java, so there's no choice, but I guess that the ability to descend from a class with nothing but virtual functions is probably the mechanism that allows googlemock to do its work...

Was it helpful?

Solution

I'm afraid not. In googlemock's documentation relating to mocking free functions, they show how to replace a free function with an equivalent function in an interface class.

It's up to you whether you think it's worth changing existing code purely to allow testing. I probably wouldn't do that generally, particularly in this case where you'd be changing a non-member function to a member-function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top