我有一个Api类,其中包含一些由Client类使用的虚拟方法。现在,我尝试使用gMock模拟Api类,以便可以在没有任何依赖关系的情况下对Client类进行单元测试。情况就是这样。
/**
* CustomEnumStatus data type below is an enum.
* enum CustomEnumStatus { OK = 0, WARNING, ERROR, CANCEL };
*/
class Api {
public:
virtual void delete();
virtual CustomEnumStatus update();
};
class MockApi : public Api {
public:
MOCK_METHOD0(delete, void());
MOCK_METHOD0(update, CustomEnumStatus());
};
/// Testing client class code
BOOST_AUTO_TEST_CASE(TestClientMethod)
{
MockApi mockApi;
Client client;
EXPECT_CALL(mockApi, update()).Times(1);
EXPECT_CALL(mockApi, delete()).Times(1);
client.clientMethod();
}
现在,这在Linux和Windows环境下可以正常工作,但是在达尔文环境下进行以下堆栈跟踪失败。
In file included from gmock-1.6.0-libc++build/include/gmock/gmock.h:58:
In file included from gmock-1.6.0-libc++build/include/gmock/gmock-actions.h:46:
gmock-1.6.0-libc++build/include/gmock/internal/gmock-internal-utils.h:355:10: error: indirection of non-volatile null pointer will be deleted, not trap [-Werror,-Wnull-dereference]
return *static_cast<typename remove_reference<T>::type*>(NULL);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gmock-1.6.0-libc++build/include/gmock/gmock-actions.h:78:22: note: in instantiation of function template specialization 'testing::internal::Invalid<CustomEnumStatus>' requested here
return internal::Invalid<T>();
^
gmock-1.6.0-libc++build/include/gmock/gmock-actions.h:190:43: note: in instantiation of member function 'testing::internal::BuiltInDefaultValue<CustomEnumStatus>::Get' requested here
internal::BuiltInDefaultValue<T>::Get() : *value_;
^
gmock-1.6.0-libc++build/include/gmock/gmock-spec-builders.h:1435:34: note: in instantiation of member function 'testing::DefaultValue<CustomEnumStatus>::Get' requested here
return DefaultValue<Result>::Get();
^
gmock-1.6.0-libc++build/include/gmock/gmock-spec-builders.h:1334:22: note: in instantiation of member function 'testing::internal::FunctionMockerBase<CustomEnumStatus ()>::PerformDefaultAction' requested here
func_mocker->PerformDefaultAction(args, call_description));
^
gmock-1.6.0-libc++build/include/gmock/gmock-spec-builders.h:1448:26: note: in instantiation of function template specialization 'testing::internal::ActionResultHolder<CustomEnumStatus>::PerformDefaultAction<CustomEnumStatus ()>' requested here
return ResultHolder::PerformDefaultAction(this, args, call_description);
^
api_gmock.cpp:22:5: note: in instantiation of member function 'testing::internal::FunctionMockerBase<CustomEnumStatus ()>::UntypedPerformDefaultAction' requested here
MockApi(
^
gmock-1.6.0-libc++build/include/gmock/internal/gmock-internal-utils.h:355:10: note: consider using __builtin_trap() or qualifying pointer with 'volatile'
return *static_cast<typename remove_reference<T>::type*>(NULL);
^
1 error generated.
如果我将Api :: update()方法的返回类型更改为任何原始类型(例如,bool,int,...),或者如果我不模拟Api :: update(),它在所有环境下都可以正常运行方法。 CustomEnumStatus类型失败。我在这里找到了确切的堆栈跟踪:https://bugzilla.mozilla.org/show_bug.cgi?id=1142396,它声称对于他们而言,升级到gMock 1.7.0可以解决此问题。
我想知道导致此问题的原因,并且是否有任何方法可以在不升级gMock版本的情况下进行修复。我正在使用gMock 1.6.0版。