Moving a mock object

306 views
Skip to first unread message

Víctor Jiménez

unread,
Nov 7, 2016, 10:32:16 AM11/7/16
to Google C++ Mocking Framework
This discussion clearly states the reasons why mock objects are not copyable. But, I wonder if there is any reason why mock objects are not moveable. In situations like the following example, it would be useful to have moveable mocks:

template<typename Impl>
class event<Impl> {
public:
  event(Impl impl = Impl{})
    : impl_{std::move(impl)}
  {}

  void foo() {
    impl_
.foo();
  }

private:
  Impl impl_;
};

class mock_event_impl {
public:
  MOCK_METHOD0(foo, void());
};

TEST(event, test) {
  mock_event_impl impl;
  EXPECT_CALL(impl, foo());

  event<mock_event_impl> event{std::move(impl)};
  event.foo();
}

After looking at the source code, I believe testing::FunctionMocker is the bit that is preventing a mock from being moveable (at least the one in my example).

Does it look useful and feasible to make mocks moveable? Are there any plans to do so?

If not, is there any suggested approach to situations like the one in the previous example?
I know I could simply store a pointer or a reference to Impl, but I would prefer "event" to manage the lifetime of "Impl" (without using unique_ptr, if possible).

Reply all
Reply to author
Forward
0 new messages