I have a method that calls pthread_create(...)
. is it possible to mock and expect the output of pthread_create
so I don't actually launch a thread?
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.
Yes. Declare mock class and functions:
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:
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:
#ifndef TESTING
#include <pthread.h>
#else
#include "pthread_mock.h"
#endif