简体   繁体   中英

how to return the error code when mocking a recvmsg function?

I am using google test and google mock to test some C code:

struct msghdr message;
int retval;

retval = recvmsg(fd,&message,0);

if(retval == EINTR || retval == EAGAIN)
{
    // do something here
}

in my mock file, I mocked the recvmsg function:

MOCK_CONST_METHOD3(recvmsg, int(int fd, struct msghdr *msg, int flags));

but how to set the errno as EINTR or EAGAIN when I set the return value of the mock function recvmsg as -1?

Google Mock docs 中有一个名为SetErrnoAndReturn(errnum, value)的标准操作。

You cannot mock free functions, but only class virtual member functions.

The proposed solution to mock free functions in the docs is to define an interface exporting a virtual member function with the same signature as the free function. Then define an implementation that wraps up the call to the free function for production code and a mock for testing. So if your testing code is pure C, it won't be a solution for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM