2016-01-14 33 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語句,但是我這樣做的方式是,不可能添加不正確的輸入,因此異常和測試從不會被觸發。如何以可能添加不正確數據的方式實施這些限制,然後對其進行測試? –

相關問題