Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking a method with throw() specifier

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?

like image 401
J R Avatar asked Feb 07 '11 14:02

J R


2 Answers

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.

like image 72
Terence Simpson Avatar answered Sep 30 '22 17:09

Terence Simpson


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.

like image 39
J R Avatar answered Sep 30 '22 17:09

J R