1
1
const test = require ( 'node:test' ) ;
2
2
const assert = require ( 'assert' ) ;
3
+ const fs = require ( 'fs' ) ;
4
+
5
+ // 模擬文件讀取操作
6
+ test . mock . method ( fs , 'readFile' , ( file , options , callback ) => {
7
+ callback ( null , 'martin\njohn\ntom' ) ;
8
+ } ) ;
9
+
10
+ // 導入需要測試的類
3
11
const { Application, MailSystem } = require ( './main' ) ;
4
12
5
- // TODO: write your tests here
6
- // Remember to use Stub, Mock, and Spy when necessary
13
+ // 測試 MailSystem 類的 write 方法
14
+ test ( 'MailSystem_write()' , ( ) => {
15
+ const mailSystem = new MailSystem ( ) ;
16
+ assert . strictEqual ( mailSystem . write ( 'martin' ) , 'Congrats, martin!' ) ;
17
+ assert . strictEqual ( mailSystem . write ( null ) , 'Congrats, null!' ) ;
18
+ assert . strictEqual ( mailSystem . write ( 48763 ) , 'Congrats, 48763!' ) ;
19
+ } ) ;
20
+
21
+ // 測試 MailSystem 類的 send 方法
22
+ test ( 'MailSystem_send()' , ( ) => {
23
+ const mailSystem = new MailSystem ( ) ;
24
+ const name = 'martin' ;
25
+ test . mock . method ( Math , 'random' , ( ) => 0.6 ) ;
26
+ assert . strictEqual ( mailSystem . send ( name , 'success' ) , true ) ;
27
+ test . mock . method ( Math , 'random' , ( ) => 0.4 ) ;
28
+ assert . strictEqual ( mailSystem . send ( name , 'fail' ) , false ) ;
29
+ } ) ;
30
+
31
+ // 測試 Application 類的 getNames 方法
32
+ test ( 'Application_getNames()' , async ( ) => {
33
+ const app = new Application ( ) ;
34
+ const nameList = [ 'martin' , 'john' , 'tom' ] ;
35
+ const names = await app . getNames ( ) ;
36
+ assert . deepStrictEqual ( names , [ nameList , [ ] ] ) ;
37
+ } ) ;
38
+
39
+ // 測試 Application 類的 getRandomPerson 方法
40
+ test ( 'Application_getRandomPerson()' , async ( ) => {
41
+ const app = new Application ( ) ;
42
+ const names = await app . getNames ( ) ;
43
+ test . mock . method ( Math , 'random' , ( ) => 0 ) ;
44
+ assert . strictEqual ( app . getRandomPerson ( ) , 'martin' ) ;
45
+ test . mock . method ( Math , 'random' , ( ) => 0.4 ) ;
46
+ assert . strictEqual ( app . getRandomPerson ( ) , 'john' ) ;
47
+ test . mock . method ( Math , 'random' , ( ) => 0.7 ) ;
48
+ assert . strictEqual ( app . getRandomPerson ( ) , 'tom' ) ;
49
+ } ) ;
50
+
51
+ // 測試 Application 類的 selectNextPerson 方法
52
+ test ( 'Application_selectNextPerson()' , async ( ) => {
53
+ const app = new Application ( ) ;
54
+ const names = await app . getNames ( ) ;
55
+ app . selected = [ 'martin' ] ;
56
+ let count = 0 ;
57
+ test . mock . method ( app , 'getRandomPerson' , ( ) => {
58
+ if ( count <= names . length ) {
59
+ return names [ 0 ] [ count ++ ] ;
60
+ }
61
+ } ) ;
62
+ assert . strictEqual ( app . selectNextPerson ( ) , 'john' ) ;
63
+ assert . deepStrictEqual ( app . selected , [ 'martin' , 'john' ] ) ;
64
+ assert . strictEqual ( app . selectNextPerson ( ) , 'tom' ) ;
65
+ assert . deepStrictEqual ( app . selected , [ 'martin' , 'john' , 'tom' ] ) ;
66
+ assert . strictEqual ( app . selectNextPerson ( ) , null ) ;
67
+ } ) ;
68
+
69
+ // 測試 Application 類的 notifySelected 方法
70
+ test ( 'Application_notifySelected()' , async ( ) => {
71
+ const app = new Application ( ) ;
72
+ app . people = [ 'martin' , 'john' , 'tom' ] ;
73
+ app . selected = [ 'martin' , 'john' , 'tom' ] ;
74
+ app . mailSystem . send = test . mock . fn ( app . mailSystem . send ) ;
75
+ app . mailSystem . write = test . mock . fn ( app . mailSystem . write ) ;
76
+ app . notifySelected ( ) ;
77
+ assert . strictEqual ( app . mailSystem . send . mock . calls . length , 3 ) ;
78
+ assert . strictEqual ( app . mailSystem . write . mock . calls . length , 3 ) ;
79
+ } ) ;
0 commit comments