1
- const TestLab = require ( 'node:test' ) ;
2
1
const Test = require ( 'node:test' ) ;
3
2
const assert = require ( 'assert' ) ;
4
3
const fileSystem = require ( 'fs' ) ;
5
4
6
5
// 模擬 fileSystem 模組的 readFile 方法
7
- TestLab . mock . method ( fileSystem , 'readFile' , ( file , options , callback ) => {
8
6
Test . mock . method ( fileSystem , 'readFile' , ( file , options , callback ) => {
9
7
callback ( null , 'ray\nnina\neric' ) ;
10
8
} ) ;
11
9
12
10
const { Application, MailSystem } = require ( './main' ) ;
13
11
14
12
// 測試 MailSystem 的 write 方法
15
- TestLab ( 'MailSystem_write()' , ( ) => {
16
13
Test ( 'MailSystem_write()' , ( ) => {
17
14
const mailSystem = new MailSystem ( ) ;
18
15
assert . strictEqual ( mailSystem . write ( 'ray' ) , 'Congrats, ray!' ) ;
@@ -21,20 +18,16 @@ Test('MailSystem_write()', () => {
21
18
} ) ;
22
19
23
20
// 測試 MailSystem 的 send 方法
24
- TestLab ( 'MailSystem_send()' , ( ) => {
25
21
Test ( 'MailSystem_send()' , ( ) => {
26
22
const mailSystem = new MailSystem ( ) ;
27
23
const name = 'ray' ;
28
- TestLab . mock . method ( Math , 'random' , ( ) => 0.7 ) ; // 成功概率高
29
24
Test . mock . method ( Math , 'random' , ( ) => 0.7 ) ;
30
25
assert . strictEqual ( mailSystem . send ( name , 'success' ) , true ) ;
31
- TestLab . mock . method ( Math , 'random' , ( ) => 0.3 ) ; // 失敗概率高
32
26
Test . mock . method ( Math , 'random' , ( ) => 0.3 ) ;
33
27
assert . strictEqual ( mailSystem . send ( name , 'fail' ) , false ) ;
34
28
} ) ;
35
29
36
30
// 測試 Application 的 getNames 方法
37
- TestLab ( 'Application_getNames()' , async ( ) => {
38
31
Test ( 'Application_getNames()' , async ( ) => {
39
32
const application = new Application ( ) ;
40
33
const nameList = [ 'ray' , 'nina' , 'eric' ] ;
@@ -43,46 +36,41 @@ Test('Application_getNames()', async () => {
43
36
} ) ;
44
37
45
38
// 測試 Application 的 getRandomPerson 方法
46
- TestLab ( 'Application_getRandomPerson()' , async ( ) => {
47
39
Test ( 'Application_getRandomPerson()' , async ( ) => {
48
40
const application = new Application ( ) ;
49
41
const names = await application . getNames ( ) ;
50
- TestLab . mock . method ( Math , 'random' , ( ) => 0 ) ;
51
42
Test . mock . method ( Math , 'random' , ( ) => 0 ) ;
52
43
assert . strictEqual ( application . getRandomPerson ( ) , 'ray' ) ;
53
- TestLab . mock . method ( Math , 'random' , ( ) => 0.4 ) ;
54
44
Test . mock . method ( Math , 'random' , ( ) => 0.4 ) ;
55
45
assert . strictEqual ( application . getRandomPerson ( ) , 'nina' ) ;
56
- TestLab . mock . method ( Math , 'random' , ( ) => 0.7 ) ;
57
46
Test . mock . method ( Math , 'random' , ( ) => 0.7 ) ;
58
47
assert . strictEqual ( application . getRandomPerson ( ) , 'eric' ) ;
59
48
} ) ;
60
49
61
50
// 測試 Application 的 selectNextPerson 方法
62
- TestLab ( 'Application_selectNextPerson()' , async ( ) => {
63
51
Test ( 'Application_selectNextPerson()' , async ( ) => {
64
52
const application = new Application ( ) ;
65
53
const names = await application . getNames ( ) ;
66
54
application . selected = [ 'ray' ] ;
67
55
let counter = 0 ;
68
- TestLab . mock . method ( application , 'getRandomPerson' , ( ) => {
69
56
Test . mock . method ( application , 'getRandomPerson' , ( ) => {
70
- if ( counter < names [ 0 ] . length ) {
71
- return names [ 0 ] [ counter ++ ] ;
72
- } else {
73
- @@ - 68 , 12 + 68 , 12 @@ TestLab ( 'Application_selectNextPerson()' , async ( ) => {
57
+ return [ 'ray' , 'nina' , 'eric' ] [ counter ++ ] ;
58
+ } ) ;
59
+ assert . strictEqual ( application . selectNextPerson ( ) , 'nina' ) ;
60
+ assert . deepStrictEqual ( application . selected , [ 'ray' , 'nina' ] ) ;
61
+ assert . strictEqual ( application . selectNextPerson ( ) , 'eric' ) ;
62
+ assert . deepStrictEqual ( application . selected , [ 'ray' , 'nina' , 'eric' ] ) ;
63
+ assert . strictEqual ( application . selectNextPerson ( ) , null ) ;
74
64
} ) ;
75
65
76
66
// 測試 Application 的 notifySelected 方法
77
- TestLab ( 'Application_notifySelected()' , async ( ) => {
78
67
Test ( 'Application_notifySelected()' , async ( ) => {
79
68
const application = new Application ( ) ;
80
69
application . people = [ 'ray' , 'nina' , 'eric' ] ;
81
70
application . selected = [ 'ray' , 'nina' , 'eric' ] ;
82
- application . mailSystem . send = TestLab . mock . fn ( application . mailSystem . send ) ;
83
- application . mailSystem . write = TestLab . mock . fn ( application . mailSystem . write ) ;
84
71
application . mailSystem . send = Test . mock . fn ( application . mailSystem . send ) ;
85
72
application . mailSystem . write = Test . mock . fn ( application . mailSystem . write ) ;
86
73
application . notifySelected ( ) ;
87
74
assert . strictEqual ( application . mailSystem . send . mock . calls . length , 3 ) ;
88
75
assert . strictEqual ( application . mailSystem . write . mock . calls . length , 3 ) ;
76
+ } ) ;
0 commit comments