8

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?

J R
  • 130
  • 1
  • 5

3 Answers3

2

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.

Terence Simpson
  • 4,440
  • 2
  • 20
  • 18
1

Google mock does not support exception specifications. The reason is that they think exception specification is mostly a misfeature and should be avoided in practice, even if you use exceptions extensively.

There are some sources that supports this point of view:

The solution would be rewriting the code as:

virtual ReturnValue FunctionName() const throw();

and then use:

MOCK_CONST_METHOD0( FunctionName, ReturnValue() );
evedovelli
  • 2,115
  • 28
  • 28
1

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.

J R
  • 130
  • 1
  • 5