如何模拟方法 return 对象与已删除的复制构造函数?

How to mock methods return object with deleted copy-ctor?

如果一个接口有一个函数来创建一个带有删除的复制构造函数的对象,如何模拟这个函数? Gmock 似乎在内部使用对象的复制构造函数。

例如

// The object with deleted copy-ctor and copy-assignment
class TTest
{
public:
    TTest() = delete;
    TTest(const TTest&) = delete;
    TTest& operator=(const TTest&) = delete;
    TTest(TTest&&) = default;
    TTest& operator=(TTest&&) = default;

    explicit TTest(int) {
    }
};

// My interface to mock
class MyInterface
{
    public:
        virtual ~MyInterface() {}
        virtual TTest GetUniqueTest() = 0;
};

// The mock
class MockMyInterface: public MyInterface{
    public:
        MOCK_METHOD0(GetUniqueTest, TTest());
}

编译错误说:

gmock/gmock-spec-builders.h:1330:20: error: use of deleted function 'TTest::TTest(const TTest&)'
     T retval(value_);
...
gmock/gmock-actions.h:190:52: error: use of deleted function 'TTest::TTest(const TTest&)'
         internal::BuiltInDefaultValue<T>::Get() : *value_;
...
gmock/internal/gmock-internal-utils.h:371:71: error: use of deleted function 'TTest::TTest(const TTest&)'
       *static_cast<volatile typename remove_reference<T>::type*>(NULL));

如果方法 returns std::unique_ptr<T>,错误是相同的,因为 std::unique_ptr<T> 也删除了 copy-ctor。

所以我的问题是:如何模拟这样的方法 return objects with deleted copy-ctors?

我正在使用 googletest v1.7、GCC 5.3.0 和 Ubuntu 14.04.1.

正如 Mine, Google Test 1.8 seems to support mocking such functions (documentation 评论中提到的那样。

至于 1.7 我找到了解决方案 here

首先,创建一个实用程序 class 来包装不可复制的对象:

template <typename T>
class Mover
{
public:
    Mover(T&& object)
      : object(std::move(object)),
        valid(true)
    {
    }

    Mover(const Mover<T>& other)
      : object(const_cast<T&&>(other.object)),
        valid(true)
    {
        assert(other.valid);
        other.valid = false;
    }

    Mover& operator=(const Mover& other)
    {
        assert(other.valid);
        object = const_cast<T&&>(other.object);
        other.valid = false;
        valid = true;
    }

    T& get()
    {
        assert(valid);
        return object;
    }

    const T& get() const
    {
        assert(valid);
        return *object;
    }

private:
    T object;
    mutable bool valid;
};

template <typename T>
inline Mover<T> Movable(T&& object)
{
    return Mover<T>(std::move(object));
}

然后创建代理模拟:

class MockMyInterface : public MyInterface
{
public:
    MOCK_METHOD0(GetUniqueTest_, Mover<TTest>());
    TTest GetUniqueTest()
    {
        return std::move(GetUniqueTest_().get());
    }
}

在这里回答我自己的问题只是为了提供最新信息。

对于 googletest 1.8.0 版或更高版本,它引入了 ByMove(...) 并原生支持 return 仅移动类型。

所以代码编译成功:

class MockMyInterface: public MyInterface{
    public:
        MOCK_METHOD0(GetUniqueTest, TTest());
}

但是在 运行 时间它抛出异常因为 gmock 不知道如何 return 默认 TTest:

C++ exception with description "Uninteresting mock function call - returning default value.
    Function call: GetUniqueTest()
    The mock function has no default action set, and its return type has no default value set." thrown in the test body.

这可以通过在模拟中设置默认操作轻松解决 class:

ON_CALL(*this, GetUniqueTest()).WillByDefault(Return(ByMove(TTest(0))));

注意:对于std::unique_ptr<T>没问题,因为它有默认构造函数,nullptr unique_ptr是由return编辑的默认。

综上所述,如果使用 googletest 1.8.0 或更高版本,我们可以:

// My interface to mock
class MyInterface
{
    public:
        virtual ~MyInterface() {}
        virtual TTest GetUniqueTest() = 0;
        virtual std::unique_ptr<int> GetUniqueInt() = 0;
};

// The mock
class MockMyInterface: public MyInterface{
    public:
        MOCK_METHOD0(GetUniqueTest, TTest());
        MOCK_METHOD0(GetUniqueInt, std::unique_ptr<int>());
        MockMyInterface() {
            ON_CALL(*this, GetUniqueTest())
                .WillByDefault(Return(ByMove(TTest(0))));
        }
};

参考:[使用仅移动类型的模拟方法] (https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#mocking-methods-that-use-move-only-types)