Can Google Mock a method with a smart pointer return type?

前端 未结 5 1321
死守一世寂寞
死守一世寂寞 2020-11-28 23:32

I have a factory that returns a smart pointer. Regardless of what smart pointer I use, I can\'t get Google Mock to mock the factory method.

The mock object is the im

5条回答
  •  生来不讨喜
    2020-11-28 23:54

    Google Mock requires parameters and return values of mocked methods to be copyable, in most cases. Per boost's documentation, unique_ptr is not copyable. You have an option of returning one of the smart pointer classes that use shared ownership (shared_ptr, linked_ptr, etc.) and are thus copyable. Or you can use a raw pointer. Since the method in question is apparently the method constructing an object, I see no inherent problem with returning a raw pointer. As long as you assign the result to some shared pointer at every call site, you are going to be fine.

提交回复
热议问题