google mock won't test correctly

Hey y'all,

I'm trying to mock an interface using google mock, but keep getting wrong results when created mock objects and testing them.
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);
}
}

implementation of the mock class:

namespace testing
{
MyClassMock:: MyClassMock(int a, int b, int c)
{
}

MyClassMock::~ MyClassMock()
{
}
}

testing class:

#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));
}

this returns:
EXPECT_CALL(mock, myFunction())
Expected: to be called at least once
Actual: never called - unsatisfied and active

Do you have an idea what am I doing wrong? Any help would be very much appreciated!


cheers,
Simon
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) So, where is it that you actually expect myFunction() to be called?

You've told the mock object that, in a successful test, you expect myFunction() to be called at least once. In other words, you've specified that your test should fail if myFunction() isn't called at least once.

And, when you run your test, it's telling you that, in fact, you aren't calling it at all. And, looking at your code, that seems to be correct, because I can't see it being called anywhere.

Actually, your test doesn't seem to be testing anything. What is it that you are testing? Why do you need to create a mock object at all?
Last edited on
Topic archived. No new replies allowed.