I'm working on a very simple Battleship game. While I've made some decent progress on the overall game, I decided it was time to start implementing some unit tests.
I went ahead and tried to include a test file in the original project but found that most people prefer to create a separate test project within the same solution (Please correct me if I'm wrong as I'm brand new to C++ ).
So I created a Test Project and setup a Single test - however, whenever I try to reference the code from my "main" project I get build errors....
From what I've read and watched tutorials on, I can use Google Test.
So I went into my Solution and Added a new Project - Choosing Test -> Google Test for the template.
My Solution now looks like this.
Additionally, I read that you can include a "path" import from one project to another. I went into the "BattleshipTest" Project and set the "Additional Include Directories" to the path of my "main" Project - "BattleshipGame".
Seemingly everything looks to be in order - I am able to build and run my BattleshipGame perfectly fine ...however, I cannot get my BattleshipTest project to build with a very simple test setup.
ShipTest.cpp
#include "pch.h"
#include "BattleshipGame/Ship.h"
#include "BattleshipGame/Coordinate.h"
#include <vector>
TEST(ShipTest, HandlesInvalidSpacesForShip) {
string shipName = "Titanic";
int shipSize = 5;
Ship subject = Ship(shipName, shipSize, 'T');
vector<Coordinate> spaces = {
Coordinate(1,1),
Coordinate(1,2),
Coordinate(1,3),
Coordinate(1,4),
Coordinate(1,5)
};
EXPECT_TRUE(subject.canFit(spaces));
}
These is the Build Output
Build started...
1>------ Build started: Project: BattleshipTests, Configuration: Debug x64 ------
1>ShipTest.obj : error LNK2019: unresolved external symbol "public: __cdecl Ship::Ship(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,char)" (??0Ship@@QEAA@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HD@Z) referenced in function "private: virtual void __cdecl ShipTest_HandlesInvalidSpacesForShip_Test::TestBody(void)" (?TestBody@ShipTest_HandlesInvalidSpacesForShip_Test@@EEAAXXZ)
1>ShipTest.obj : error LNK2019: unresolved external symbol "public: bool __cdecl Ship::canFit(class std::vector<class Coordinate,class std::allocator<class Coordinate> >)" (?canFit@Ship@@QEAA_NV?$vector@VCoordinate@@V?$allocator@VCoordinate@@@std@@@std@@@Z) referenced in function "private: virtual void __cdecl ShipTest_HandlesInvalidSpacesForShip_Test::TestBody(void)" (?TestBody@ShipTest_HandlesInvalidSpacesForShip_Test@@EEAAXXZ)
1>ShipTest.obj : error LNK2019: unresolved external symbol "public: __cdecl Coordinate::Coordinate(int,int)" (??0Coordinate@@QEAA@HH@Z) referenced in function "private: virtual void __cdecl ShipTest_HandlesInvalidSpacesForShip_Test::TestBody(void)" (?TestBody@ShipTest_HandlesInvalidSpacesForShip_Test@@EEAAXXZ)
1>C:\Users\Drbal\source\repos\BattleshipGame\x64\Debug\BattleshipTests.exe : fatal error LNK1120: 3 unresolved externals
1>Done building project "BattleshipTests.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
========== Build started at 5:21 PM and took 00.328 seconds ==========
I've also tried adding a dependency from my "BattleshipTest" Project to my "BattleshipGame" project but that doesn't seem to help.
Also Including the contents of Ship.h and Coordinate.h just in case it helps.
Ship.h
#pragma once
#include <vector>
#include <string>
using namespace std;
class Coordinate;
class Ship {
public:
int size;
string name;
char marker;
Ship(string shipName, int shipSize, char shipMarker);
bool canFit(vector<Coordinate> spaces);
};
Coordinate.h
#pragma once
#include <string>
using namespace std;
class BoardPosition;
class Coordinate {
public:
int x;
int y;
Coordinate(int x, int y);
bool isVacant(string board[11][11]);
bool hasEmptySpace(string board[11][11]);
BoardPosition asBoardPosition();
};