如果一個接口有一個函數來創建一個刪除copy-ctor的對象,如何模擬這個函數? Gmock似乎在內部使用對象的拷貝構造函數。如何模擬方法返回刪除copy-ctor對象?
E.g.
// 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-ctors的對象?
我使用googletest v1.7,GCC 5.3.0和Ubuntu 14.04.1。
偉大的答案,它工作得很好! – Mine
谷歌測試** 1在我看來。8 **似乎支持嘲笑這些功能:https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#mocking-methods-that-use-move-only-types它可能更新爲googletest v1.8是個好主意。 – Mine
@我是的,我也發現這個,但沒有時間來驗證:)也許當你確認你也可以自我回答;) – BartoszKP