如何使用GoogleMock使用模拟功能填充结构?
我有一个函数:
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
人页面告诉我“ 参数addr是sockAddr结构的指针。此结构用对等插座的地址填充,如通信层所知。“因此,我将使用以下方式调用以下功能:
struct sockaddr_storage clientAddr;
struct sockaddr* sa{(sockaddr*)&clientAddr};
clientLen = sizeof(clientAddr);;
accept_sock = accept(listen_sock, (struct sockaddr*)&clientAddr, &clientLen);
使用Mocking 接受
我想填充clientAddr
使用:
struct sockaddr_in* sa_in{(sockaddr_in*)&clientAddr};
sa_in->sin_family = AF_INET;
sa_in->sin_port = htons(50000);
inet_pton(AF_INET, "192.168.1.2", &sa_in->sin_addr);
To return a mocked pointer to a filled structure I would use:
EXPECT_CALL(mocked_sys_socket, accept(listen_sock, _, Pointee(clientLen)))
.WillOnce(DoAll(SetArgPointee<1>(*sa), Return(accept_sock)));
但这不是我在这里需要的。 clientAddr
结构已经给出。
什么动作而不是setArgpointee&lt;(*sa)
我必须用来返回提供的结构client> clientaDdr
?
I have a function for example:
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
The man page tell me "The argument addr is a pointer to a sockaddr structure. This structure is filled in with the address of the peer socket, as known to the communications layer." So I would call the function with:
struct sockaddr_storage clientAddr;
struct sockaddr* sa{(sockaddr*)&clientAddr};
clientLen = sizeof(clientAddr);;
accept_sock = accept(listen_sock, (struct sockaddr*)&clientAddr, &clientLen);
With mocking accept
I would like to fill clientAddr
with:
struct sockaddr_in* sa_in{(sockaddr_in*)&clientAddr};
sa_in->sin_family = AF_INET;
sa_in->sin_port = htons(50000);
inet_pton(AF_INET, "192.168.1.2", &sa_in->sin_addr);
To return a mocked pointer to a filled structure I would use:
EXPECT_CALL(mocked_sys_socket, accept(listen_sock, _, Pointee(clientLen)))
.WillOnce(DoAll(SetArgPointee<1>(*sa), Return(accept_sock)));
But that isn't what I need here. The clientAddr
structure is already given.
What action instead of SetArgPointee<1>(*sa)
I have to use to return with filling the provided structure clientAddr
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
iiuc,您希望通过呼叫
Accept
,Accept
的第二个参数,该参数当前是sa_int
将设置为sa_next
的值。如果是这样,我可以想到两个选择:
实时示例: https://godbolt.org.org.org/z/3h4ffcnd7
IIUC, you want that as a result of a call to
accept
, the second parameter ofaccept
, which is currentlysa_int
to be set to the value ofsa_next
.If that's the case, I can think of two options:
Live example: https://godbolt.org/z/3h4ffcnd7
ARI的答案显示了解决方案。这里有一个完整的示例是我现在使用的实现。
通过执行测试,我得到了:
参考:
依赖注入和用GoogleMock模拟的实践示例
The answer of Ari shows the solution. To have a complete example here is the implementation I use now.
With executing the test I get:
Reference:
Practical example for Dependency Injection and mocking with Googlemock