Reputation: 45
I'm new to google mock and I'm trying to mock an interface, but keep getting a linker error with undefined symbols for architecture x86_64
Here's my simplified code:
I have the following in a .h file:
namespace Mynamespace
{
class IMyInterface
{
public:
virtual ~ IMyInterface() {};
virtual void myFunction() = 0;
};
}
this in another .h file:
#include <gmock/gmock.h>
#include <IMyInterface.h>
namespace testing
{
class MyClassMock : public IMyInterface
{
public:
~ MyClassMock();
MyClassMock(int, int, int);
MOCK_METHOD0(myFunction, void());
};
}
and this in my Test Case .cpp file:
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <IMyInterface.h>
namespace testing
{
TEST(MyClassMock, myFunction)
{
MyClassMock mcm(0,0,0);
}
}
Do you have an idea what am I doing wrong? Any help would be very much appreciated!
cheers, Simon
EDIT:
Unfortunately the mock still doesn't seem to work. After I added the implementation like this:
namespace testing
{
MyClassMock:: MyClassMock(int a, int b, int c)
{
}
MyClassMock::~ MyClassMock()
{
}
}
"myFunction" will not be called when I do
#include "MyClassMock.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using ::testing::AtLeast;
using namespace testing;
TEST(MyClassTest, canCallFunction)
{
MyClassMock mock(0,0,0);
EXPECT_CALL(mock, myFunction())
.Times(AtLeast(1));
}
returning: EXPECT_CALL(mock, myFunction()) Expected: to be called at least once Actual: never called - unsatisfied and active
Upvotes: 1
Views: 787
Reputation: 6125
You have to provide implementations for MyClassMock::MyClassMock(int, int, int)
and MyClassMock::~MyClassMock()
.
On a side not, you should use ""
rather than <>
when you #include
your own headers. E.g. #include "IMyInterface.h"
not #include <IMyInterface.h>
. That way, the compiler will search in the current directory prior to the system include path.
Upvotes: 1