mercredi 22 avril 2015

Mock function that returns move-only type using gmock

I have the following mock class set up using gmock:

class MockFoo
{
public:
  MOCK_METHOD3 (bar, future<void> (const string& arg1, bool arg2, const string& arg3));

  std::future<void> getFuture (const string& arg1)
  {
     auto found_device = promise_map.find (arg1);
     if (found_device != promise_map.end ())
     {
        found_device->second->set_value ();
        promise_map.erase (found_device);
     }
     promise_map [arg1] = std::make_shared<std::promise<void>> ();
     return promise_map [arg1]->get_future ();
  }
  std::unordered_map<string, std::shared_ptr<std::promise<void>>> promise_map;
};

I am trying to test a production class that takes a Foo by template param and instantiates a Foo as a member:

// In production
ClassToTest<Foo> production_object;
// In test
ClassToTest<MockFoo> class_to_test;

I'd like to validate in a test that class_to_test.methodToTest() calls bar(...) on its MockFoo object:

TEST_F (ClassToTestTest, methodToTestCallsBar)
{
    ON_CALL (class_to_test.foo, bar (_, _, _))
        .WillByDefault (Invoke (&class_to_test.foo, &MockFoo::getFuture));

    class_to_test.methodToTest ();
}

This fails to compile in Visual Studio 2013:

Error C2280: 'std::future<void>::future(const std::future<void>&)': attempting to reference a deleted function

...which makes sense, because std::future is noncopyable. However, I need this to work. Reply #6 here suggests that mock methods are able to return move-only types; however, I have not been able to find any documentation or examples that show this. How can I effectively mock my function that returns a move-only type?

Aucun commentaire:

Enregistrer un commentaire