I am trying to Google mock a virtual method which has a throw() specifier. The original function looks like this:
virtual ReturnValue FunctionName() const throw();
I am getting the compiler error: looser throw specifier for 'virtual FunctionSignature'
Here is the code I have tried thus far:
MOCK_CONST_METHOD0( FunctionName, ReturnValue() );
MOCK_CONST_METHOD0( FunctionName, ReturnValue() throw() );
MOCK_CONST_METHOD0( FunctionName, ReturnValue() ) throw(); // Gives a different error entirely.
I've tried just about every other combination I can think of, but these are the ones which seem most logical. How do I go about Google mocking a method with a throw() specifier?
From what I can tell, you'd have to use the "internal" GMOCK_METHOD0_
macro, and use:
GMOCK_METHOD0_(, const throw(), , FunctionName, ReturnValue)
as MOCK_CONST_METHOD0(m, F)
is #defineed to GMOCK_METHOD0_(, const, , m, F)
, gmock/gmock-generated-function-mockers.h#644 and gmock/gmock-generated-function-mockers.h#347 defines that.
My solution: create an implementation of the virtual function which consists solely of passing through to a mocked method.
MOCK_CONST_METHOD0( MockFunctionName, ReturnValue() );
virtual ReturnValue FunctionName() const throw()
{
return MockFunctionName();
}
Then, whenever you need to write an Expect_Call or do anything for that method, just refer to MockFunctionName.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With