Reputation: 1148
I am trying to test my C library with google test but I'm having trouble mocking functions with the fff.h
framework. This is my file structure:
.
├── Makefile.am
├── configure.ac
├── include
│ ├── Makefile.am
│ └── public_header.h
├── src
│ └── libmylib
│ ├── Makefile.am
│ ├── private_functions.c
│ ├── private_functions.h
│ ├── libmylib.la
│ └── libmylib.c
└── test
└── libmylib_test
├── Makefile.am
├── fff.h
└── test.cc
I want to mock a function from the header private_functions.h
which is used in a function from the public_header.h
using the fff.h
framework.
public_function()
{
private_function(); //This function is the one I want to mock.
}
My test looks like this:
#include "gtest/gtest.h"
#include "public_header.h"
#include "fff.h"
extern "C" {
#include "private_functions.h"
}
DEFINE_FFF_GLOBALS;
FAKE_VALUE_FUNC(int, function, char *, char *);
class libtest : public testing::Test
{
public:
virtual void SetUp()
{
RESET_FAKE(function);
}
virtual void TearDown()
{
}
};
TEST_F(libtest, test_fff)
{
public_function("val1", "val2");
EXPECT_EQ(function_fake.call_count, 1);
}
...
When I run make it says that the private_function()
is defined multiple times.
My test/libmylib_test/MakeFile.am
looks like this:
LIBSRC = $(top_srcdir)/src/libstorage
check_PROGRAMS = libmylib_test
libstorage_test_SOURCES = test.cc
libstorage_test_CFLAGS = $(AM_CFLAGS)
libstorage_test_CXXFLAGS = -I$(top_srcdir)/include -I$(LIBSRC) -std=c++11 $(AM_CPPFLAGS)
libstorage_test_LDFLAGS = $(AM_LDFLAGS) -static -pthread
libstorage_test_LDADD = $(top_srcdir)/src/libmylib/libmylib.la
Upvotes: 6
Views: 5507
Reputation: 1891
This is a linking problem, you can't include the source file with the functions you're mocking/faking. The easiest fix IMO is to compile the code under test as a shared library and link your tests against that. When you run the tests, the dynamic linker will doe its magic and won't load from the dynamic the faked functions because they're already present in the application code (through the mocked functions).
Upvotes: 1
Reputation: 1148
I have a solution but I'm not sure if it's the best one.
When I compile my library I want to test it generates .o
files for every .c
file I have. As I only want to test libmylib.c
I linked only libmylib_la-libmylib.o
instead of the .la
file.
The generated .o
files in src/libmylib/
were:
libmylib_la-libmylib.o
libmylib_la-private_functions.o
My updated MakeFile.am
in ./test/libmylib_test/
looks like this now:
LIBSRC = $(top_srcdir)/src/libstorage
check_PROGRAMS = libmylib_test
libstorage_test_SOURCES = test.cc
libstorage_test_CFLAGS = $(AM_CFLAGS)
libstorage_test_CXXFLAGS = -I$(top_srcdir)/include -I$(LIBSRC) -std=c++11 $(AM_CPPFLAGS)
libstorage_test_LDFLAGS = $(AM_LDFLAGS) -static -pthread
libstorage_test_LDADD = $(top_srcdir)/src/libmylib/libmylib_la-libmylib.o
As I had another function in the private_functions.h
I had to mock that one too because it was not defined elsewhere.
After these updates the tests compiled without problems and ran fine.
Test file now looks like this:
#include "gtest/gtest.h"
#include "public_header.h"
#include "fff.h"
extern "C" {
#include "private_functions.h"
}
DEFINE_FFF_GLOBALS;
FAKE_VALUE_FUNC(int, function, char *, char *);
FAKE_VOID_FUNC(other_func, char *);
...
Upvotes: 2