Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中测试包含字符串的方法#_C#_.net_Unit Testing_Oop - Fatal编程技术网

C# 如何在C中测试包含字符串的方法#

C# 如何在C中测试包含字符串的方法#,c#,.net,unit-testing,oop,C#,.net,Unit Testing,Oop,我有一个应用程序,允许你添加学生和讲师的详细信息,搜索并显示它们,等等。这是一个大学作业,我必须测试我创建的五种方法。首先,我不知道如何测试一个包含字符串的方法,因为我见过的所有测试方法都涉及到一个银行账户应用程序,测试取款和存款方法似乎很容易,因为你只需要加减数字。我根本不知道如何测试我的(例如)add讲师()方法。如果我正确创建了一个状态类,但程序似乎仍然认为它是一个未处理的异常,那么我尝试得到一种抛出异常的方法。如何修复异常以便正确处理,以及如何测试这些其他方法 以下是该应用程序的主要入口

我有一个应用程序,允许你添加学生和讲师的详细信息,搜索并显示它们,等等。这是一个大学作业,我必须测试我创建的五种方法。首先,我不知道如何测试一个包含字符串的方法,因为我见过的所有测试方法都涉及到一个银行账户应用程序,测试取款和存款方法似乎很容易,因为你只需要加减数字。我根本不知道如何测试我的(例如)add讲师()方法。如果我正确创建了一个状态类,但程序似乎仍然认为它是一个未处理的异常,那么我尝试得到一种抛出异常的方法。如何修复异常以便正确处理,以及如何测试这些其他方法

以下是该应用程序的主要入口点以及所有方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBSManagement
{
    public class College: Staff
    {
        public static List<Student> students = new List<Student>();
        public static List<Lecturer> lecturers = new List<Lecturer>();

        public static void Main()
        { 
            int choice;
            bool seeAgain = true;

                do
                {
                    Console.WriteLine("Press");
                    Console.WriteLine("1: To add a student");
                    Console.WriteLine("2: To add a lecturer");
                    Console.WriteLine("3: To search for a lecturer or student");
                    Console.WriteLine("4: To show the details of all enrolled students");
                    Console.WriteLine("5: To show the names of all lecturers");
                    Console.WriteLine("6: To show payroll details for a lecturer");
                    Console.WriteLine("7: To quit");
                    int.TryParse(Console.ReadLine(), out choice);

                    switch (choice)
                    {
                        case 1:
                            AddStudent();
                            break;
                        case 2:
                            AddLecturer();
                            break;
                        case 3:
                            SearchPerson();
                            break;
                        case 4:
                            ShowStudents();
                            break;
                        case 5:
                            ShowLecturers();
                            break;
                        case 6:
                            ShowPayrollDetails();
                            break;
                        case 7:
                            seeAgain = false;
                            break;
                        default:
                            Console.WriteLine("Invalid option selected");
                            break;
                    }
                } while (seeAgain);
            }
        public static void AddStudent()
        {
            Student student = new Student();
            Console.WriteLine("Enter student name:");
            if (Console.ReadLine() != null)
            {
                student.Name = Console.ReadLine();
            }
            else throw new ArgumentNullException("Please enter a name");

            Console.WriteLine("Enter student address:");
            student.Address = Console.ReadLine();
            Console.WriteLine("Enter student phone number:");
            student.Phone = Console.ReadLine();
            Console.WriteLine("Enter student email:");
            student.Email = Console.ReadLine();
            Console.WriteLine("Enter student PPSN:");
            student.PPSN = Console.ReadLine();
            Console.WriteLine("Enter student status (postgrad or undergrad):");
            EnterStat:
                string stat = Console.ReadLine().ToLower();
                if (stat == "postgrad" || stat == "undergrad")
                {
                    student.Status = (Status)Enum.Parse(typeof(Status), stat);
                }
                else
                {
                    Console.WriteLine("Please enter either postgrad or undergrad:");
                    goto EnterStat;
                }
            Console.WriteLine("Enter student ID:");
            int inStudentID;
            int.TryParse(Console.ReadLine(), out inStudentID);
            student.StudentID = inStudentID;
            students.Add(student);
        }

        public static void AddLecturer()
        {
            Lecturer lecturer = new Lecturer();
            Console.WriteLine("Enter lecturer name:");
            lecturer.Name = Console.ReadLine();
            Console.WriteLine("Enter lecturer address:");
            lecturer.Address = Console.ReadLine();
            Console.WriteLine("Enter lecturer phone number:");
            lecturer.Phone = Console.ReadLine();
            Console.WriteLine("Enter lecturer email:");
            lecturer.Email = Console.ReadLine();
            Console.WriteLine("Enter lecturer PPSN:");
            lecturer.PPSN = Console.ReadLine();
            Console.WriteLine("Enter lecturer ID:");
            lecturer.ID = Console.ReadLine();
            Console.WriteLine("Enter salary:");
            lecturer.Salary = decimal.Parse(Console.ReadLine());
            Console.WriteLine("Enter subject taught:");
            lecturer.SubjectTaught = Console.ReadLine().ToLower();
            lecturers.Add(lecturer);
        }

        public static void SearchPerson()
        {
            int searchChoice = 0;
            int studentSearch = 0;
            int lecturerSearch = 0;
            Console.WriteLine("Press:");
            Console.WriteLine("1 to search for a student");
            Console.WriteLine("2 to search for a lecturer");
            int.TryParse(Console.ReadLine(), out searchChoice);

            switch (searchChoice)
            {
                //search students
                case 1:
                    Console.WriteLine("Press:");
                    Console.WriteLine("1 to search by name");
                    Console.WriteLine("2 to search by student number");
                    int.TryParse(Console.ReadLine(), out studentSearch);

                    switch (studentSearch)
                    {
                        case 1:
                            Console.WriteLine("Enter student name:");
                            string studentNameSearch = Console.ReadLine();
                            bool sFound = false;
                            foreach (Student student in students)
                            {
                                if (student.Name.Contains(studentNameSearch))
                                {
                                    Console.WriteLine(student.ToString());
                                    sFound = true;
                                    break;
                                }
                            }
                            if (sFound == false)
                            {
                                Console.WriteLine("Student name not found");
                            }
                            break;

                        case 2:
                            int studentIDSearch;
                            bool IDFound = false;
                            Console.WriteLine("Enter student number:");
                            int.TryParse(Console.ReadLine(), out studentIDSearch);
                            foreach (Student student in students)
                            {
                                if (student.StudentID.Equals(studentIDSearch))
                                {
                                    Console.WriteLine(student.ToString());
                                    IDFound = true;
                                    break;
                                }
                            }
                            if (IDFound == false)
                            {
                                Console.WriteLine("Student name not found");
                            }
                            break;

                        default:
                            Console.WriteLine("Invalid option selected");
                            break;
                    }
                    break;
                //search lecturers
                case 2:
                    Console.WriteLine("Press:");
                    Console.WriteLine("1 to search by name");
                    Console.WriteLine("2 to search by course taught");
                    int.TryParse(Console.ReadLine(), out lecturerSearch);

                    switch (lecturerSearch)
                    {
                        case 1:
                            Console.WriteLine("Enter lecturer name:");
                            string lecturerNameSearch = Console.ReadLine();
                            bool lFound = false;
                            foreach (Lecturer lecturer in lecturers)
                            {
                                if (lecturer.Name.Contains(lecturerNameSearch))
                                {
                                    Console.WriteLine(lecturer.ToString());
                                    lFound = true;
                                    break;
                                }
                            }
                            if (lFound == false)
                            {
                                Console.WriteLine("Lecturer name not found");
                            }
                            break;

                        case 2:
                            Console.WriteLine("Enter course taught:");
                            string lecturerSubjectSearch = Console.ReadLine().ToLower();
                            bool subjectFound = false;
                            foreach (Lecturer lecturer in lecturers)
                            {
                                if (lecturer.SubjectTaught.Contains(lecturerSubjectSearch))
                                {
                                    Console.WriteLine(lecturer.ToString());
                                    subjectFound = true;
                                    break;
                                }
                            }
                            if (subjectFound == false)
                            {
                                Console.WriteLine("Subject not found");
                            }
                            break;

                        default:
                            Console.WriteLine("Invalid option selected");
                            break;
                    }
                    break;

                default:
                    Console.WriteLine("Invalid option selected");
                    break;
            }
        }

        public static void ShowStudents()
        {
            //sort list by name
            List<Student> SortedStudents = students.OrderBy(o => o.Name).ToList();

            foreach (Student student in SortedStudents)
            {
                Console.WriteLine(student);
            }
        }

        public static void ShowLecturers()
        {
            //sort list by name
            List<Lecturer> SortedLecturers = lecturers.OrderBy(o => o.Name).ToList();

            foreach (Lecturer lecturer in SortedLecturers)
            {
                Console.WriteLine(lecturer.Name);
            }
        }

        public static void ShowPayrollDetails()
        {
            Console.WriteLine("Enter lecturer name:");
            string lecturerNameSearch = Console.ReadLine();
            for (int i = 0; i < lecturers.Count; i++)
            {
                if (lecturers[i].Name == lecturerNameSearch)
                {
                    Console.WriteLine(lecturers[i].PayrollDetails());
                }
                else
                {
                    Console.WriteLine("Lecturer name not found");
                }
            }
        }
    }
}
这是学生班。我试图让它抛出一个例外,如果任何人进入一个状态以外的研究生或本科生。这些是枚举

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBSManagement
{
    public class Student : Person
    {
        private Status status;
        //auto-implemented properties
        public Status Status
        {
            get
            {
                return status;
            }
            set
            {
                if (value == Status.undergrad || value == Status.postgrad)
                {
                    status = value;
                }
                else throw new ArgumentException("Error: please select undergrad or postgrad");
            }
        }
        public int StudentID { get; set; }

        //empty constructor
        public Student() { }

        //constructor with parameters
        public Student(string name, string address, string phone, string email, string ppsn, Status status, int studentId)
            :base(name, address, phone, email, ppsn)
        {
            Status = status;
            StudentID = studentId;
        }

        //overridden ToString() method
        public override string ToString()
        {
            return string.Format("Name: {0}\nStudent Number: {1}\nAddress: {2}\nPhone: {3}\nEmail: {4}\nStatus: {5}",
                Name, StudentID, Address, Phone, Email, Status);
        }
    }
}
您可以测试您的代码,但这些测试将非常脆弱(而且,正如@Scott Chamberlain所指出的,不清楚它们将证明什么)

您需要做的是将丑陋的
Console.ReadLine()
隐藏在您可以“编程”控制的东西后面
Func
最理想:

public static void AddStudent(Func<string> consoleReader)
{
    Student student = new Student();

    Console.WriteLine("Enter student name:");
    student.Name = Console.ReadLine();
    // ...        
}
publicstaticvoidaddstudent(Func控制台阅读器)
{
学生=新生();
Console.WriteLine(“输入学生姓名:”);
student.Name=Console.ReadLine();
// ...        
}
这样,您的测试就会变成:

[Test]
void TestAddStudent()
{
    var n = 0;
    var strings = new[] {
        "Name", 
        "123 Fake St", 
        "0851234567", 
        "fake@address.com", 
        "7895459R",
        // ...
    };

    Func<string> consoleReader = () => strings[n++];

    var student = AddStudent(consoleReader);

    Assert.AreEqual(strings[0], student.Name);
    // ...
}
[测试]
无效测试学生()
{
var n=0;
var strings=new[]{
“姓名”,
“123假街”,
"0851234567", 
"fake@address.com", 
“7895459R”,
// ...
};
Func consolerereader=()=>字符串[n++];
var student=AddStudent(控制台阅读器);
Assert.AreEqual(字符串[0],student.Name);
// ...
}
您可以测试您的代码,但这些测试将非常脆弱(而且,正如@Scott Chamberlain所指出的,不清楚它们将证明什么)

您需要做的是将丑陋的
Console.ReadLine()
隐藏在您可以“编程”控制的东西后面
Func
最理想:

public static void AddStudent(Func<string> consoleReader)
{
    Student student = new Student();

    Console.WriteLine("Enter student name:");
    student.Name = Console.ReadLine();
    // ...        
}
publicstaticvoidaddstudent(Func控制台阅读器)
{
学生=新生();
Console.WriteLine(“输入学生姓名:”);
student.Name=Console.ReadLine();
// ...        
}
这样,您的测试就会变成:

[Test]
void TestAddStudent()
{
    var n = 0;
    var strings = new[] {
        "Name", 
        "123 Fake St", 
        "0851234567", 
        "fake@address.com", 
        "7895459R",
        // ...
    };

    Func<string> consoleReader = () => strings[n++];

    var student = AddStudent(consoleReader);

    Assert.AreEqual(strings[0], student.Name);
    // ...
}
[测试]
无效测试学生()
{
var n=0;
var strings=new[]{
“姓名”,
“123假街”,
"0851234567", 
"fake@address.com", 
“7895459R”,
// ...
};
Func consolerereader=()=>字符串[n++];
var student=AddStudent(控制台阅读器);
Assert.AreEqual(字符串[0],student.Name);
// ...
}

如果要进行测试,将UI与逻辑分离会更容易。例如,您可以采用MVC模式或类似的模式。首先构建所有数据对象,如讲师、学生等。这些对象将是您的数据模型。然后添加操作这些数据对象的逻辑或控件。控制组件中可能有一个
add讲师(..)
方法。最后,创建一个UI或视图,它可以与它们交互,而不会像代码中那样完全交织在一起。关于测试,您将主要为控制组件和模型中的方法编写测试。有很多东西需要测试。采用“添加讲师”方法:

  • 名称是否超过3个字符
  • 至少有两个名字吗?(也许这是一个太强的假设?)
  • 电话号码的格式是否正确
  • 电子邮件的格式是否正确
  • 讲师的身份证只有号码吗?(不过,我希望讲师ID是由您的系统生成的)
  • PPSN的格式是否正确
  • 薪水是正数吗
  • 薪水是不是太高了
  • 工资是偶数吗`
  • 在向讲师添加新讲师时,是否真的添加了新讲师?(通常情况下,您不会检查此项。我们信任基本集合,除非您自己编写。)
  • 等等

如果要进行测试,将UI与逻辑分离会更容易。例如,您可以采用MVC模式或类似的模式。首先构建所有数据对象,如讲师、学生等。这些对象将是您的数据模型。然后添加操作这些数据对象的逻辑或控件。控制组件中可能有一个
add讲师(..)
方法。最后,创建一个UI或视图,它可以与它们交互,而不会像代码中那样完全交织在一起。关于测试,您将主要为控制组件和模型中的方法编写测试。有很多东西需要测试。采用“添加讲师”方法:

  • 名称是否超过3个字符
  • 至少有两个名字吗?(也许这是一个太强的假设?)
  • 电话号码的格式是否正确
  • 电子邮件的格式是否正确
  • 讲师的身份证只有号码吗?(不过,我希望讲师ID是由您的系统生成的)
  • PPSN的格式是否正确
  • 薪水是正数吗
  • 薪水是不是太高了
  • 工资是偶数吗`
  • 在向讲师添加新讲师时,是否真的添加了新讲师?(通常情况下,您不会检查此项。我们信任基本集合,除非您自己编写。)
  • 等等
您不能(轻松地)测试UI交互,通常为了使测试更容易,您需要通过一个抽象的测试来抽象所有UI交互。我看不出你实际上可以“测试”的
add讲师是什么。测试是