So I am trying to learn how to write unit Tests and i stumbled upon the problem, that I don't understand how to create mock objects. Here's my example: I have this class:
class FooChild
{
public:
void doThis();
bool doThat(int n, double x);
};
And this is a method in another class I want to test:
#include "FooFighter.h"
#include "FooChild.h"
void FooFighter::doSomething()
{
FooChild fooChild;
fooChild.doThis();
fooChild.doThat(4, 5);
}
I want to test Things like if it called the method and how many times. The Google mock documentary says, that only Abstract classes with virtual methods can be mocked. That's why i tried to create a parent class of FooChild, like this:
class Foo
{
public:
virtual void doThis() = 0;
virtual bool doThat(int n, double x) = 0;
};
And then create a mock class of Foo like this:
#include "gmock/gmock.h"
class MockFoo : public Foo
{
public:
MOCK_METHOD(void, doThis, (), (override));
MOCK_METHOD(bool, doThat, (int n, double x), (override));
};
Then I tried to write the Test for doSomething:
TEST_F(FooFighterTest, doSomethingTest)
{
MockFoo mock_foo
mock_foo.doThis()
.Times(1);
}
Clearly this doesn't work and I have the feeling that I completely misunderstood how mocks work, but I just can't seem to find a nice and simple explanation on how to create mocks. Any help or advice would be great. Also my approach on how to test void functions like this might be completely wrong, so any advice on how to test functions that don't return anything would be great too.