如何模拟方法返回删除了copy-ctor的对象? [英] How to mock methods return object with deleted copy-ctor?

查看:50
本文介绍了如何模拟方法返回删除了copy-ctor的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果接口具有创建带有删除的copy-ctor的对象的功能,该如何模拟该功能? Gmock似乎在内部使用对象的副本构造函数.

If an interface has a function to create an object with deleted copy-ctor, how to mock this function? Gmock seems to use the object's copy constructor internally.

例如

// 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));

如果该方法返回std::unique_ptr<T>,则错误是相同的,因为std::unique_ptr<T>也删除了copy-ctor.

If the method returns std::unique_ptr<T>, the error is the same since std::unique_ptr<T> has deleted copy-ctor as well.

所以我的问题是:如何模拟这样的方法,这些方法会返回带有删除的复制指针的对象?

So my question is: how to mock such methods that return objects with deleted copy-ctors?

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

I'm using googletest v1.7, GCC 5.3.0, and Ubuntu 14.04.1.

推荐答案

如Google 我的矿山的评论中所述测试1.8似乎支持模拟这样的功能(文档).

As mentioned in the comments by Mine, Google Test 1.8 seems to support mocking such functions (documentation).

对于1.7,我找到了一个解决方案此处.

As for 1.7 I've found a solution here.

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

First, create an utility class to wrap non-copyable objects:

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));
}

,然后创建一个代理模拟:

and then create a proxy-mock:

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

这篇关于如何模拟方法返回删除了copy-ctor的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆