简体   繁体   English

gmock SetArgReferee:设置一个不可复制不可移动的object

[英]gmock SetArgReferee: Set a non-copyable non-moveable object

I'm using gmock and have mocked a function that takes boost::beast::http::response_parser as an out parameter.我正在使用 gmock 并模拟了一个 function ,它将boost::beast::http::response_parser作为输出参数。 Function signature looks something like: Function 签名看起来像:

error_code readFromSocket(boost::beast::http::response_parser& resp);

Now, to test this function, I mocked and use EXPECT_CALL like:现在,为了测试这个 function,我模拟并使用EXPECT_CALL如下:

boost::beast::http::response_parser respObject;
// set status code in respObject
EXPECT_CALL(mockObject, readFromSocket(_))
    .Times(1)
    .DoAll(SetArgReferee<0>(respObject), Return(err::success));

And it returns me an operator= deleted compilation error.它返回给我一个operator= deleted compilation错误。 I have tried various combinations using ByRef , ByMove , using std::ref but none of them works and I understand why they don't work.我尝试了使用ByRefByMove ,使用std::ref的各种组合,但它们都不起作用,我理解它们为什么不起作用。

Has anyone ever ran into a similar situation before and know how to solve this error?有没有人遇到过类似的情况并且知道如何解决这个错误? Please let me know if you need clarification.如果您需要澄清,请告诉我。

Thank you.谢谢你。

EDIT编辑

This is what readFromSocket looks like:这就是readFromSocket的样子:

template<typename T>
error_code readFromSocket(beast::http::response_parser<T>& resp,
                          boost::asio::yield_context yield)
{
    // read from socket using beast::http::async_read
    return success;
}

This is how I call it.我就是这样称呼它的。

beast::http::response_parser<beast::http::string_body> resp;
resp.body_limit((std::numeric_limits<std::string::size_type>::max)());
auto ec = readFromSocket(resp, yield);

// after this I do error handling and response status code handling.

I was trying to achieve the same behaviour and WithArg was the key.我试图实现相同的行为,而WithArg是关键。 This is an example of what I ended up with:这是我最终得到的一个例子:

#include <gmock/gmock.h>
#include <gtest/gtest.h>


using namespace testing;

struct NonCopyable {
    NonCopyable() = default;
    NonCopyable(const NonCopyable&) = delete;
    NonCopyable& operator=(const NonCopyable&) = delete;

    int a;
};

struct WorkWithNonCopyable {
    virtual ~WorkWithNonCopyable() = default;

    virtual bool modify_non_copyable(NonCopyable& nc) = 0;
};

struct WorkWithNonCopyableMock {
    MOCK_METHOD1(modify_non_copyable, bool(NonCopyable& nc));
};

TEST(WorkWithNonCopyableTest, NormalizedRoiDetectorTest) {
    WorkWithNonCopyableMock work_with_non_copyable_mock{};

    EXPECT_CALL(work_with_non_copyable_mock, modify_non_copyable(_))
        .WillOnce(DoAll(WithArg<0>([](auto& arg) { arg.a = 42; }), Return(true)));

    NonCopyable non_copyable;
    ASSERT_TRUE(work_with_non_copyable_mock.modify_non_copyable(non_copyable));
    ASSERT_EQ(non_copyable.a, 42);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用原始指针的情况下动态引用不同的不可复制/不可移动 object? - How to dynamically reference to different non-copyable/non-moveable object without using raw pointer? 如何将不可移动和不可复制的函数返回值获取到数组中 - How to get a non-moveable and non-copyable function return value into an array 如何在嵌套向量中构造不可复制、不可移动的 class? - How to contruct non-copyable, non-moveable class in a nested vector? 可移动但不可复制的例外 - Moveable but Non-Copyable Exceptions 是否有可能让“命名构造函数”返回一个私有构造的、不可移动的、不可复制的 std::optional<T> ? - Is it possible to have a "named constructor" return a privately-constructed, non-moveable, non-copyable std::optional<T>? 为什么通过显式不可移动和隐式不可复制类型的值返回向量不会产生编译错误? - Why returning of vector by value of explicitly non-moveable and implicitly non-copyable type does NOT produce a compilation error? 无法在C ++ 17中构造一个包含const int和不可复制(但可移动)对象的对 - Cannot construct a pair containing a const int and a non-copyable (but moveable) object in C++17 可以使用不可复制的成员作为使对象不可复制的替代方法吗? - Can a non-copyable member be used as alternative to make an object non-copyable? 替换不可复制,不可移动的对象 - Replacing a non-copyable, non-movable object 使用显式构造函数返回不可复制的不可移动对象 - Returning non-copyable non-movable object with explicit constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM