Hi,
i entounter some problem with the unit testing and gmock.
My objectif is to test some interaction with the filesystem for example if a directory doesn't exist.
Here is a small example method :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
bool Foo::foo()
{
DIR *rep;
Dirent interface;
// Aims, test if a exeption append here.
rep = interface.opendirInt("/home/manticore/Downloads/");
struct dirent *lecture;
while ((lecture = readdir(rep)))
{
if (strcmp(lecture->d_name,"test.txt") == 0)
{
printf("Test found\n");
return true;
}
}
return false;
}
|
I have create a interface for the dirent.h :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
/*
* DirentInterface.h
*
* Created on: Jan 16, 2013
* Author: Marc Romanens
*/
#ifndef DIRENTINTERFACE_H_
#define DIRENTINTERFACE_H_
#include <dirent.h>
class DirentInterface
{
public:
// Prototype: DIR * opendir (const char *dirname)
virtual DIR* opendirInt (const char *dirname) = 0;
};
class Dirent : public DirentInterface
{
public:
DIR* opendirInt (const char *dirname)
{
return opendir(dirname);
}
};
#endif /* DIRENTINTERFACE_H_ */
|
And here is my mock object :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
/*
* MockDirent.h
*
* Created on: Jan 16, 2013
* Author: Marc Romanens
*/
#ifndef MOCKDIRENT_H_
#define MOCKDIRENT_H_
#include "gmock/gmock.h"
#include "../../../sourceProject/src/DirentInterface.h"
class MockDirent : public Dirent
{
public:
MOCK_CONST_METHOD1(opendirInt, DIR*(const char *dirname));
};
#endif /* MOCKDIRENT_H_ */
|
Finnaly here is my testing code :
1 2 3 4 5 6 7
|
TEST_F(FooTest, foo)
{
MockDirent dirent;
EXPECT_CALL(dirent, opendirInt("/home/manticore/Downloads/")).Times(1);//))
bool test = foo.foo();
ASSERT_TRUE(test);
}
|
My objectif is to remplace the call of opendir in Foo::foo() by my mock, but for begin i trying to get EXPECT_CALL, and the test fail, my opendirInt method is never call.
Thanks you, (i'am glad to join this forum).
p.s. Sorry for my poor english ;)