0

我对 CLI 中的谷歌测试完全陌生。刚刚设法设置并将 google test 与 Visual Studio 2012 集成。但是,当我尝试将我的项目的头文件包含在我的 tester.h 文件中时(因为我想测试该头文件中的函数),我遇到了以下问题错误:

Error   2   error LNK2028: unresolved token (0A0006FB) "public: __thiscall ExpenseTracker::ExpenseTracker(void)" (??0ExpenseTracker@@$$FQAE@XZ) referenced in function "private: virtual void __thiscall enter_settings_user_login_Test::TestBody(void)" (?TestBody@enter_settings_user_login_Test@@$$FEAEXXZ)  C:\Users\Jacky\Desktop\EzXpns3\test_project\main.obj    test_project
Error   3   error LNK2019: unresolved external symbol "public: __thiscall ExpenseTracker::ExpenseTracker(void)" (??0ExpenseTracker@@$$FQAE@XZ) referenced in function "private: virtual void __thiscall enter_settings_user_login_Test::TestBody(void)" (?TestBody@enter_settings_user_login_Test@@$$FEAEXXZ)   C:\Users\Jia Wei\Desktop\EzXpns3\test_project\main.obj  test_project

我试过包括依赖项 gtestd.lib、kernel32.lib、user32.lib、advapi32.lib、Ws2_32.lib

问题稍微解决了。我没有使用expenseTracker.h 和ExpenseTracker.cpp,而是将ExpenseTracker.h 的所有实现放在头文件本身中,并且所有内容都编译得很好并进行了测试运行。但是,我的整个项目都是建立在头文件和 cpp 文件上的,为了测试目的而重做所有事情不是很明智,对吧?有人可以帮忙吗?

下面附上我的头文件。

//tester.h

#include "gtest/gtest.h" //include to use Google Unit test's stuff
#include "C:\Users\Jacky\Desktop\EzXpns3\Source - testing\EzXpns2\ExpenseTracker.h"

using namespace std;

class ExpenseTracker;
/************************************************************************/

/* We write test cases here */

/************************************************************************/

TEST(basic_test, add_simple_route)
{
    ASSERT_EQ(1, 1);
}

TEST(enter_settings, user_login)
{
    ExpenseTracker :: ExpenseTracker();
    //loadUserInfo();
    string username = "XXX_XXX";
    string password = "12345";
    //myTracker -> loadUserInfo();
    //bool result = myTracker -> login(username, password);
    //ASSERT_EQ(true, result);
}

void runTest(int argument_count, char** argument_vars)
{
    testing::InitGoogleTest(&argument_count, argument_vars); //initialize GTest
    RUN_ALL_TESTS();
    std::getchar(); //pause the program after all the testing
}

//main.cpp

#include "tester.h"
#include <cstdio>


using namespace System;
using namespace testing;

int main(int argument_count, char** argument_vars)
{
    //int argc;
    //char** argv;
    //runTest(argc, argv);
    //InitGoogleTest(argc, argv);   

    testing::InitGoogleTest(&argument_count, argument_vars); //initialize GTest
    RUN_ALL_TESTS();
    std::getchar();


    return 0;
}

//ExpenseTracker.h,我要包含的文件

#ifndef _EXPENSETRACKER_H
#define _EXPENSETRACKER_H

#include <string>
#include <vector>
#include "user2.h"

using namespace std;

class ExpenseTracker
{
private:
    vector<User*> allMyUsers;

public:
    ExpenseTracker(); //empty constructor
    void addUser(User*);
    int findUser(string);
    bool login(string, string);
    void loadUserInfo();
    User* getUser(string);
    int getUserSize();
};

#endif;
4

1 回答 1

-1

您需要为构造函数提供一个实现。换句话说,你应该改变:

 ExpenseTracker(); //empty constructor

进入:

ExpenseTracker() {} // empty constructor

但是,由于构造函数是空的,您不妨将其完全删除!

于 2013-03-15T10:26:54.857 回答