2016-01-14 31 views
0

我有一个应用程序,它允许您添加学生和讲师的详细信息,并搜索它们,并显示它们等。这是一个大学作业,我必须测试五个我创建的方法。首先,我不知道如何测试涉及字符串的方法,因为我所看到的所有测试方法都涉及银行账户应用程序,并且测试取款和存款方法似乎很容易,因为您只需添加和减去数字。我不确定如何测试我的(例如)AddLecturer()方法。如果我创建的Status类输入正确,但是程序似乎仍然认为它是未处理的异常,我试图获取其中一种方法来引发异常。如何修复异常,以便正确处理,以及如何测试这些其他方法?如何在C#中测试涉及字符串的方法

这里是所有方法的应用程序的主要入口点。

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 Microsoft.VisualStudio.TestTools.UnitTesting; 
using DBSManagement; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace DBSManagement.Tests 
{ 
    [TestClass()] 
    public class CollegeTests 
    { 
     [TestMethod()] 
     [ExpectedException(typeof(ArgumentException))] 
     public void AddStudentTest() 
     { 
      //arrange 
      string s = "student"; 
      Status status = (Status)Enum.Parse(typeof(Status), s); 
      //act 
      Student student1 = new Student("Name", "123 Fake St", "0851234567", "[email protected]", "7895459R", status, 12345678); 
      //assert 
      //handled by exception 
     } 

     [TestMethod()] 
     public void AddLecturerTest() 
     { 
      Assert.Fail(); 
     } 

     [TestMethod()] 
     public void SearchPersonTest() 
     { 
      Assert.Fail(); 
     } 

     [TestMethod()] 
     public void ShowStudentsTest() 
     { 
      Assert.Fail(); 
     } 

     [TestMethod()] 
     public void ShowLecturersTest() 
     { 
      Assert.Fail(); 
     } 

     [TestMethod()] 
     public void ShowPayrollDetailsTest() 
     { 
      Assert. 

这是学生班。我试图让任何人进入除了postgrad或者本科以外的状态都会抛出异常。这些是枚举。

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); 
     } 
    } 
} 
+2

你不能(轻易)测试UI交互,通常使测试更容易抽象你通过[交互服务](https://msdn.microsoft.com/en-us/library/gg405494(v = pandp.40).aspx#sec11)进行的所有UI交互。 Hostestly我没有看到'AddLecturer'你实际上可以“测试”。测试应该证明某些事情是真实的,'AddLecturerTest'应该证明什么事实? –

+0

这几乎是我的想法。尽管这个该死的任务占了25%的分数。我认为她希望我们使这些方法抛出自定义异常。我试图做的,但他们崩溃的程序。 –

+1

你用哪种方法抛出异常?你可以分享它的代码吗? – Martin

回答

1

可以测试你的代码,但这些测试将是非常脆弱的(并且,作为@Scott张伯伦指出,这将不会是清楚他们将被证明)。

你需要做的是在你“编程”控制的东西后面“隐藏”那个丑陋的Console.ReadLine()Func<string>将是理想的:

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

    Console.WriteLine("Enter student name:"); 
    student.Name = Console.ReadLine(); 
    // ...   
} 

有了这个,你的测试变得喜欢的东西:

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

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

    var student = AddStudent(consoleReader); 

    Assert.AreEqual(strings[0], student.Name); 
    // ... 
} 
+0

感谢您的帮助。我不熟悉这种技术。我试过了,我的switch语句现在要求AddStudent()的参数。 –

1

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

  • 名称是否超过3个字符?
  • 是否至少有两个名字? (也许这是一个太强的假设?)
  • 电话号码是否格式正确?
  • 电子邮件格式是否正确?
  • 讲师ID是唯一的数字吗? (虽然,我希望讲师ID是由你的系统生成的)
  • PPSN格式良好吗?
  • 工资是正值吗?
  • 工资是不是很可观?
  • 薪水甚至是一个数字?`
  • 当给lecturers添加新讲师时,是否确实添加了? (通常,你永远不会检查。我们相信基本的集合,除非你自己写的。)
+0

非常感谢。我不太熟悉MVC,除了我研究过的CodeIgniter的一小部分。我不积极如何实施这些测试。我确定这个人添加“postgrad”或“undergrad”的方式是在大学课堂上有一个if语句,但是我这样做的方式是,不可能添加不正确的输入,因此异常和测试从不会被触发。如何以可能添加不正确数据的方式实施这些限制,然后对其进行测试? –

相关问题