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?
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.
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:
Herb Sutter (author of "Exceptional C++" and "More Exceptional C++"): A Pragmatic Look at Exception Specifications http://www.gotw.ca/publications/mill22.htm
Anders Hejlsberg (chief designer of C#): The Trouble with Checked Exceptions http://www.artima.com/intv/handcuffs.html
The solution would be rewriting the code as:
virtual ReturnValue FunctionName() const throw();
and then use:
MOCK_CONST_METHOD0( FunctionName, ReturnValue() );
链接地址: http://www.djcxy.com/p/4792.html
上一篇: 将具有循环引用的JavaScript对象字符串化(转换为JSON)
下一篇: 用throw()说明符嘲笑一个方法