[英]Mocking pthread_create using gmock
I have a method that calls pthread_create(...)
.我有一个调用pthread_create(...)
的方法。 is it possible to mock and expect the output of pthread_create
so I don't actually launch a thread?是否可以模拟并期望pthread_create
的 output 所以我实际上并没有启动线程?
I am asking this because the entire class is a Mock Object and as soon as I delete the object at the end of the test case the thread segments.我问这个是因为整个 class 是一个模拟 Object 并且一旦我在测试用例末尾删除 object 线程段。
Yes.是的。 Declare mock class and functions:声明模拟 class 和功能:
struct phtread_interface
{
virtual int pthread_create(...) = 0;
... // other methods
};
class pthread_mock : public phtread_interface
{
public:
MOCK_METHOD1(pthread_create, int(...));
....
};
pthread_interface *current_pthread_mock;
void set_current_pthread_mock(phtread_interface *mock)
{
current_pthread_mock = mock;
}
int pthread_create(...)
{
return current_pthread_mock->pthread_create(...);
}
In every test function do following:在每次测试 function 中执行以下操作:
pthread_mock mock_obj;
set_current_pthread_mock(&mock_obj);
// set expectations over mock_obj, use pthread_create ...
In source file with pthread_create
add conditional include like:在带有pthread_create
的源文件中添加条件包括:
#ifndef TESTING
#include <pthread.h>
#else
#include "pthread_mock.h"
#endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.