我对CLI中的Google测试完全陌生。刚设法将google测试与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;
答案 0 :(得分:-1)
您需要为构造函数提供实现。换句话说,你应该改变:
ExpenseTracker(); //empty constructor
成:
ExpenseTracker() {} // empty constructor
但是,由于构造函数是空的,您可以完全删除它!