Starting out with GTest and C++ Unit Tests

Hey All,

I'm just beginning with Google Test. I think I understand the premise, and want to get started with my first test. Say I have a header file that looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Dice
{
public:
	explicit Dice(const short int argTotal = 5);
	~Dice();

	void roll();
	void print() const;
	void releaseAllDie();
	void toggleDice(int argIndex);

	std::vector<Die>::iterator begin();
	std::vector<Die>::iterator end();

private:
	short int total;
	std::vector<Die> collection;
};


You can probably imagine what each of these functions does. Note that all of them are void. I'd like to begin by testing roll(), which calls Die.roll() for each Die in the collection. Note, I have access to each die face via

1
2
3
4
Dice myHand;
std::vector<int> faces;
for(auto x : myHand)
   faces.push_back(x.face());


I'd like to write a test that mocks out the Dice, like above and test if each of the die faces are between 0 - 6, but I'm not sure exactly how that translates into Gtest code.

I'd done the following:

1
2
3
4
5
//DiceTest.h
#include "gtest\gtest.h"
#include "Dice.h"

void rollTest();


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//DiceTest.cpp
#include "DiceTest.h"

std::vector<int> mockRollDice()
{
	Dice myDice;
	myDice.roll();
	std::vector<int> faces;
	for (auto x : myDice)
		faces.push_back(x.face());
}


TEST(RollTest, randomNumbers)
{
	std::vector<int> faces = mockRollDice();

	for(auto x : faces)
		//expect x is eq to 0 - 6
}


Could someone help me figure out how to write the commented part? Is it a valid test? Should I instead, write it for Die?
Topic archived. No new replies allowed.