@@ -2,41 +2,34 @@ const test = require('node:test');
22const assert = require ( 'assert' ) ;
33const { MyClass, Student } = require ( './main' ) ;
44
5- test ( " Test MyClass's addStudent" , async ( t ) => {
5+ test ( ' Test MyClass\ 's addStudent' , ( ) => {
66 const myClass = new MyClass ( ) ;
77 const student = new Student ( ) ;
8- student . setName ( "John Doe" ) ;
9-
10- // 測試添加學生是否成功
8+ student . setName ( "Test Student" ) ;
119 const index = myClass . addStudent ( student ) ;
12- assert . strictEqual ( index , 0 , "Should return the index of the added student" ) ;
10+ assert . strictEqual ( index , 0 ) ; // 應該正確添加學生並返回索引0
1311
14- // 測試添加非Student實例
15- const nonStudent = { name : "Fake Student" } ;
16- const nonStudentIndex = myClass . addStudent ( nonStudent ) ;
17- assert . strictEqual ( nonStudentIndex , - 1 , "Should return -1 for non-Student instances" ) ;
12+ const invalidStudent = { } ;
13+ const invalidIndex = myClass . addStudent ( invalidStudent ) ;
14+ assert . strictEqual ( invalidIndex , - 1 ) ; // 傳入非Student實例應返回-1
1815} ) ;
1916
20- test ( "Test MyClass's getStudentById" , async ( t ) => {
21- const myClass = new MyClass ( ) ;
17+
18+ test ( 'Test Student\'s setName' , ( ) => {
2219 const student = new Student ( ) ;
23- student . setName ( "Jane Doe" ) ;
24- myClass . addStudent ( student ) ;
25-
26- // 測試通過有效ID獲取學生
27- const fetchedStudent = myClass . getStudentById ( 0 ) ;
28- assert . strictEqual ( fetchedStudent . getName ( ) , "Jane Doe" , "Should fetch the correct student by ID" ) ;
29-
30- // 測試使用無效ID獲取學生
31- const invalidStudent = myClass . getStudentById ( - 1 ) ;
32- assert . strictEqual ( invalidStudent , null , "Should return null for invalid IDs" ) ;
20+ student . setName ( "Test Student" ) ;
21+ assert . strictEqual ( student . name , "Test Student" ) ; // 設置的名字應該符合預期
22+
23+ student . setName ( 123 ) ; // 傳入非字符串應不修改名字
24+ assert . strictEqual ( student . name , "Test Student" ) ; // 名字應該保持不變
3325} ) ;
3426
35- test ( "Test Student's setName and getName" , async ( t ) => {
27+
28+
29+ test ( 'Test Student\'s getName' , ( ) => {
3630 const student = new Student ( ) ;
37- student . setName ( "John Smith" ) ;
38- assert . strictEqual ( student . getName ( ) , "John Smith" , "setName and getName should correctly set and return the student's name" ) ;
31+ assert . strictEqual ( student . getName ( ) , '' ) ; // 應該返回空字符串,因為名字未設置
3932
40- student . setName ( 12345 ) ; // 嘗試使用非字符串設置名字
41- assert . notStrictEqual ( student . getName ( ) , 12345 , "setName should not accept non-string values ") ;
33+ student . setName ( "Test Student" ) ;
34+ assert . strictEqual ( student . getName ( ) , "Test Student ") ; // 設置名字後應該返回設置的名字
4235} ) ;
0 commit comments