[英]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.我尝试了使用ByRef
, ByMove
,使用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.谢谢你。
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.