top of page

First Mock - gtest and gmock part 5

Hello :)

I know you are waiting for it so firstly we need to understand what is a mock and for what we are doing it.

gMock was designed to create objects from abstract classes. It mean you have abstract class and pure abstract method – we can not test it by gTest. What should we do? Use gMock – because you know which parameters type was used and what should be type of results. That mean you try to prevent making mistakes from developers site which create abstract methods. Look at code bellow how it is working, but before read few tips and at the end of page you have code from my console.

Build tests in different folder than code, it can be just different folder. It is important to have separated tests and code. You can create/fix cmake for building it if necessary and next use make.

Download: https://github.com/google/googletest

Make mock class for human at the beginning.

MockHuman.h

 

#pragma once

#include "human.h"

#include <gmock/gmock.h>

class MockHuman: public human

{

public:

MOCK_METHOD1(move, void(int));

MOCK_METHOD1(attack, void(human*));

};

 

Now we have our magic with human mock, archer and warrior. You should analyze the code and create your own version based on this knowledge

Test.cc

 

#define private public

#define protected public

#include <gtest/gtest.h>

#include <gmock/gmock.h>

#include "human.h"

#include "archer.h"

#include "warrior.h"

#include "MockHuman.h"

using namespace ::testing;

archer Maniek;

warrior Bronek;

TEST(humanTest, PositiveNos) {

EXPECT_EQ(0, human::getCounter());

EXPECT_NE(-1, human::getCounter());

EXPECT_NE(1, human::getCounter());

human::incrementCounter();

EXPECT_EQ(1, human::getCounter());

}

TEST(archerTest, PositiveNos) {

EXPECT_EQ(1, archer::getCounter());

EXPECT_NE(0, archer::getCounter());

EXPECT_NE(2, archer::getCounter());

EXPECT_EQ(1, Maniek.getCounter());

EXPECT_NE(0, Maniek.getCounter());

EXPECT_NE(2, Maniek.getCounter());

archer::incrementCounter();

EXPECT_EQ(2, archer::getCounter());

EXPECT_EQ(100, Bronek.getHealth());

Maniek.attack(&Bronek);

EXPECT_EQ(90, Bronek.getHealth());

EXPECT_EQ(0, Maniek.position);

Maniek.move(2);

EXPECT_EQ(2, Maniek.position);

}

TEST(warriorTest, PositiveNos) {

EXPECT_EQ(2, warrior::getCounter());

EXPECT_NE(1, warrior::getCounter());

EXPECT_NE(3, warrior::getCounter());

EXPECT_EQ(2, Bronek.getCounter());

EXPECT_NE(1, Bronek.getCounter());

EXPECT_NE(3, Bronek.getCounter());

warrior::incrementCounter();

EXPECT_EQ(3, warrior::getCounter());

EXPECT_EQ(100, Maniek.getHealth());

Bronek.attack(&Maniek);

EXPECT_EQ(90, Maniek.getHealth());

EXPECT_EQ(0, Bronek.position);

Bronek.move(2);

EXPECT_EQ(2, Bronek.position);

}

void MockTests(MockHuman *humanMock) {

humanMock->move(1);

humanMock->attack(&Maniek);

}

TEST(mockHuman, PositiveNos) {

MockHuman humanMock;

EXPECT_CALL(humanMock,move(1))

.Times(Exactly(1));

EXPECT_CALL(humanMock,attack(&Maniek))

.Times(Exactly(1));

MockTests(&humanMock);

}

int main(int argc, char** argv) {

testing::InitGoogleMock(&argc, argv);

testing::InitGoogleTest(&argc, argv);

return RUN_ALL_TESTS();

}

 

Run tests:

Subscribe me!

Be sure that you read all.

  • Facebook Clean
  • Black Twitter Icon
  • Black Instagram Icon
  • Black YouTube Icon
Recent Posts
bottom of page