2015-12-04 92 views
2

我是C++的初學者,正在編寫一個Snake for uni,並且必須運行單元測試。僅供參考,我在Xcode 7.1.1上編碼。GoogleTest C++ - 測試夾具

我試着在我的機器上運行樣本測試,但是在爲我的蛇創建夾具時遇到問題。這裏是我的代碼有:

#include "gtest/gtest.h" 
#include "calc.h" 
#include "Serpent.h" 
#include "Map.h" 
#include "Main.h" 
using namespace std; 
using namespace sf; 

class Serpent_test_fixture: public ::testing::Test 
{public: 
Serpent* serpent_test; 
Map* map_test; 

Serpent_test_fixture(){ 
    serpent_test = new Serpent(true); 
    RenderWindow window(VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32), "SnakeTest", Style::None); 
    map_test = new Map("", window, false); 
} 

virtual void SetUp(){ 
    map_test->updateGameField(0, 0, HEAD_EAST); 
    serpent_test->setHead(*map_test, false); 
    int size_init = serpent_test->getSize(); 
} 


virtual void TearDown(){ 

} 

~Serpent_test_fixture(){ 
    delete serpent_test; 
    delete map_test; 
} 
}; 
TEST_F(Serpent_test_fixture, cherry_action) 
{ 
map_test->updateGameField(0,0,CHERRY); 
Tiles head_tile_test = HEAD_EAST; 
serpent_test->fruit_action(*map_test, head_tile_test); 
EXPECT_EQ(20, 20); 
} 
int main(int argc, char * argv[]) 
{ 
::testing::InitGoogleTest(&argc, argv); 
return RUN_ALL_TESTS(); 
} 

我的理解是這樣的,我從我的類毒蛇創建我的蛇(法語蛇),然後創建我從我的課地圖的地圖。

UpdateGameField更新地圖(0,0)中的圖塊並在其上放置「HEAD_EAST」。

不管怎麼說,這是我的消息有:

Undefined symbols for architecture x86_64: 
    "Map::updateGameField(int, int, Tiles)", referenced from: 
     Serpent_test_fixture_test_cherry_action_Test::TestBody() in main.o 
     Serpent_test_fixture::SetUp() in main.o 
    "Map::Map(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, sf::RenderWindow const&, bool)", referenced from: 
    Serpent_test_fixture::Serpent_test_fixture() in main.o 
    "Map::~Map()", referenced from: 
    Serpent_test_fixture::SetUp() in main.o 
    Serpent_test_fixture::~Serpent_test_fixture() in main.o 
    "Serpent::fruit_action(Map&, Tiles&)", referenced from: 
    Serpent_test_fixture_test_cherry_action_Test::TestBody() in main.o 
    "Serpent::getSize()", referenced from: 
    Serpent_test_fixture::SetUp() in main.o 
    "Serpent::setHead(Map, bool)", referenced from: 
    Serpent_test_fixture::SetUp() in main.o 
    "Serpent::Serpent(bool)", referenced from: 
    Serpent_test_fixture::Serpent_test_fixture() in main.o 
    "Serpent::~Serpent()", referenced from: 
    Serpent_test_fixture::~Serpent_test_fixture() in main.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

誰能幫助?這是我第一次使用Google測試,並且我很難使其工作。

+0

在main.o目標文件中,鏈接程序找不到那些提到的方法的編譯代碼。你可能有一個serpent.o和一個map.o.你需要告訴鏈接器使用這些,而不僅僅使用main.o中的編譯代碼。 – Henrik

+0

請在你的問題中發佈實際的錯誤信息。鏈接到圖片索引不好,使有相似問題的人更難找到這個問題。 –

+0

感謝您的意見,我發佈了實際的錯誤消息。 @Henrik,我該怎麼做?我很遺憾沒有使用鏈接器的經驗... – EdouM

回答

1

這裏的基本問題是如何協調測試項目相對於要測試的生產代碼和測試框架的依賴關係。依賴關係驅動構建系統(編譯器,鏈接器等)。

您可能想看看我的C++ Now! 2014年在Test-Driven Development in C++上的演示文稿。它使用Boost.Test而不是谷歌測試,但是顯示了一個基於CMake的配方來編排生產代碼和測試代碼之間的依賴關係。本次研討會旨在讓您在計算機中遵循,複製演示文稿中的步驟 - 我提供您在每個步驟中使用的代碼。

的依賴看起來像這樣對於一個典型的測試項目:

  • 測試可執行
    • 生產代碼進行測試(靜態庫或共享庫)
    • 測試框架

在我的研討會資料中,我將展示如何使用CMake創建這些依賴關係,使用Boost.Test作爲測試框架,但原理與谷歌測試相同,配方也幾乎相同。