在编写 c++的单体测试,但是在用 mock 的时候出了点问题。
//在 mock 类里面,函数已经被 mock 了
MOCK_METHOD1(unRegisterListener, Return<EnCommonFuncResult>(EnCommonListenerID));
//unRegisterListener 的函数签名是
virtual Return<EnMeterFuncResult> unRegisterListener(EnMeterListenerID serviceId)//这个 Return 是 android::hardware::Return
//现在想进行插桩,EnCommonFuncResult 就是一个 enum class EnCommonFuncResult : uint8_t
Return<EnCommonFuncResult> returnOk= EnCommonFuncResult::EN_COMMON_RESULT_OK;//这个 Return 是 android::hardware::Return
EXPECT_CALL(mockICommon, unRegisterListener(_)).WillOnce(::testing::Return(returnOk));//这儿出错
//以下为报错信息
error: call to implicitly-deleted copy constructor of 'android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>'
system/libhidl/base/include/hidl/Status.h:200:5: note: copy constructor is implicitly deleted because 'Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>' has a user-declared move constructor
Return(Return &&other) = default;
抱歉 c++有点菜,实在没搞懂这个 android::hardware::Return 该怎么用
1
amiwrong123 OP ```cpp
template<typename T> class Return : public details::return_status { private: T mVal {}; public: Return(T v) : details::return_status(), mVal{v} {} Return(Status s) : details::return_status(s) {} // move-able. // precondition: "this" has checked status // postcondition: other is safe to destroy after moving to *this. Return(Return &&other) = default; Return &operator=(Return &&) = default; ~Return() = default; operator T() const { assertOk(); return mVal; } T withDefault(T t) { return isOk() ? mVal : t; } }; ``` 这是 android::hardware::Return 的源码,因为有这句 Return(Return &&other) = default;,我就不能调用拷贝构造函数了,因为被隐式删除了。 求大佬解答了 |
2
amiwrong123 OP error: call to implicitly-deleted copy constructor of 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Result' (aka 'android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>')
virtual Result Perform(const ArgumentTuple&) { return value_; } ^~~~~~ external/googletest/googlemock/include/gmock/gmock-actions.h:575:14: note: in instantiation of member function 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Perform' requested here explicit Impl(const linked_ptr<R>& value) ^ external/googletest/googlemock/include/gmock/gmock-actions.h:557:26: note: in instantiation of member function 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Impl' requested here return Action<F>(new Impl<R, F>(value_)); ^ TestSender.cpp:54:66: note: in instantiation of function template specialization 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::operator Action<android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>' requested here EXPECT_CALL(mockICommon, unRegisterListener(_)).WillOnce(::testing::Return(EnCommonFuncResult::EN_COMMON_RESULT_OK)); ^ system/libhidl/base/include/hidl/Status.h:200:5: note: copy constructor is implicitly deleted because 'Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>' has a user-declared move constructor Return(Return &&other) = default; 给一下附言 1 的完整错误信息吧。 可能是因为 gmock 的::testing::Return 的内部逻辑吧,导致我这个地方总是会调用到 android::hardware::ReturnReturn<EnComdCommonFuncResult>。gmock 的源码我也去看了,但是太难懂了。快哭了 |
3
billyzs 2019-10-24 23:44:11 +08:00
试试
``` Return<EnCommonFuncResult> returnOk{EnCommonFuncResult::EN_COMMON_RESULT_OK} ``` |
4
xiaoloudoufu 2019-10-25 10:07:52 +08:00
你可以看一下文档:https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md ,对于 move-only 的类型应该要使用 ByMove()
|
5
amiwrong123 OP @xiaoloudoufu
谢谢,我也是刚看见有这个,等会我试试 |
6
amiwrong123 OP @xiaoloudoufu
果然 ,用了这个就好了,怪自己没好好文档了 |