【问题标题】:Visual Studio 2010 Unit Testing: Referenced Project Class Not Being RecognizedVisual Studio 2010 单元测试:引用的项目类未被识别
【发布时间】:2018-06-25 15:23:11
【问题描述】:

我正在尝试在 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
        }
    };
}

在构建单元测试项目时,我收到一条错误消息: “First 是一个未声明的标识符”。

如果我将“public ref class”值从“UnitTest1”更改为我的类名“First”,我会收到以下错误: "'getAge' 不是 'TestProject5::First' 的成员"

【问题讨论】:

  • 请不要发布您的代码或错误消息的图片,但在您的问题中包含所有相关信息作为文本。
  • 您可能缺少using namespace 指令。还要确保 First 类实际上是 C++/CLI 类(一个 ref 类)并且它是公共的。而且您实际上打算创建一个托管单元测试,C++/CLI 类不适合“非常基本的 C++ 类”这个绰号。
  • 这是你的代码,我看不到。使用对象浏览器。
  • @HansPassant 我的“第一”类不是 Ref 类。我需要做什么来修复它? --我不知道 ref 类是什么
  • @Capricorn 我现在有代码。对此感到抱歉

标签: c++ unit-testing visual-studio-2010


【解决方案1】:

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

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

【讨论】:

  • 当我这样做时,我得到错误:“无法打开包含文件:'First.h':没有这样的文件或目录”。我不明白你的第二个问题。这个项目是C++,什么是CLI?
【解决方案2】:

我通过右键单击我的测试项目并选择: 属性>常规>CommonLanguageRuntimeSupport。然后,我将“安全 MSIL 公共语言运行时支持 (/clr:safe)”更改为“公共语言运行时支持 (/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);
        }
    };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多