2

我有一个调用pthread_create(...). 是否可以模拟并期望输出,pthread_create所以我实际上并没有启动线程?

我问这个是因为整个类是一个模拟对象,一旦我在测试用例结束时删除对象,线程段。

4

1 回答 1

4

是的。声明模拟类和函数:

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(...);
}

在每个测试功能中执行以下操作:

pthread_mock mock_obj;
set_current_pthread_mock(&mock_obj);

// set expectations over mock_obj, use pthread_create ...    

在源文件中pthread_create添加条件包括:

#ifndef TESTING
#include <pthread.h>
#else
#include "pthread_mock.h"
#endif
于 2012-12-10T16:29:44.580 回答