Visual Studio 2010 单元测试:无法识别引用的项目类



我正在尝试为Visual Studio 2010中一个非常基本的C++类进行基本的单元测试。我已经测试了该类,一切正常。但是,当我制作测试项目时,我无法识别该类。

我的类头文件:

#include <string>
#include <iostream>
using namespace std;
#ifndef FIRST_H
#define FIRST_H
class First
{
private:
string name;
int age;
public:
// Constructors
First(); // default constructor
First(string n, int a); // constructor with parameters
void ChangeAge(int newAge);
void ChangeName(string newName);
void getName();
void getAge();
};
#endif

我的类 cpp 文件:

#include "First.h"
// Constructors
First :: First()// default constructor
{
name = "No Name";
age = 0;
}
First :: First(string n, int a) // constructor with parameters
{
name = n;
age = a;
}
//Manipulators
void First :: ChangeAge(int newAge)
{
age = newAge;
}
void First :: ChangeName(string newName)
{
name = newName;
}
// Observers
void First :: getName()
{
cout << "The name of this student is " << name << endl;
//cin >> name;
}
void First :: getAge()
{
cout << "The age of this student is " << age << endl;
}

这是我的基本单元测试: 注意:所有这些都是自动生成的,除了 void TestMethod1(( 的主体

#include "stdafx.h"
using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;
namespace TestProject5
{
[TestClass]
public ref class UnitTest1
{
public: 
[TestMethod]
void TestMethod1()
{
First person;    // Sets Age to 0
person.getAge(); // Should Display 0
}
};
}

在构建单元测试项目时,我收到一个错误,说: "第一个是未声明的标识符"。

如果我将"公共引用类"值从"UnitTest1"更改为我的类名"First",则会出现以下错误: "'getAge' 不是 'TestProject5::First' 的成员">

您需要在单元测试文件中#include "First.h"

也就是说,您确定的目标是 C++/CLI 吗?

我通过右键单击我的测试项目并选择: 属性>常规>通用语言运行时支持。然后,我将"Safe MSIL Common Language Runtime Support (/clr:safe("更改为"Common Language RunTime Support (/clr("。 之后,我仍然收到错误,所以我将我的 ref 类名从回生成的默认名称。我还删除了"命名空间 TestProject5"以及大括号。这修复了我的所有错误。

这是我的新代码:


#include "stdafx.h"
#include "First.cpp"
using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;
//namespace TestProject4
[TestClass]
public ref class UnitTest5
{
public: 
[TestMethod]
void TestMethod1()
{
First person;
int age = person.getAge();
Assert.AreEqual(0, age);
}
};

最新更新