1
1
const { expect } = require ( 'chai' ) ;
2
2
const sinon = require ( 'sinon' ) ;
3
- const fs = require ( 'fs' ) ;
3
+ const fs = require ( 'fs' ) . promises ;
4
4
const { MailSystem, Application } = require ( './main_test' ) ;
5
5
6
6
describe ( 'MailSystem' , ( ) => {
@@ -16,13 +16,13 @@ describe('MailSystem', () => {
16
16
const name = 'Alice' ;
17
17
const context = mailSystem . write ( name ) ;
18
18
19
- sinon . stub ( Math , 'random' ) . returns ( 0.6 ) ;
19
+ sinon . stub ( Math , 'random' ) . returns ( 0.6 ) ; // 假設成功
20
20
let result = mailSystem . send ( name , context ) ;
21
21
expect ( result ) . to . be . true ;
22
22
23
23
Math . random . restore ( ) ;
24
24
25
- sinon . stub ( Math , 'random' ) . returns ( 0.4 ) ;
25
+ sinon . stub ( Math , 'random' ) . returns ( 0.4 ) ; // 假設失敗
26
26
result = mailSystem . send ( name , context ) ;
27
27
expect ( result ) . to . be . false ;
28
28
@@ -31,37 +31,33 @@ describe('MailSystem', () => {
31
31
} ) ;
32
32
33
33
describe ( 'Application' , ( ) => {
34
- beforeEach ( ( ) => {
34
+ beforeEach ( async ( ) => {
35
35
sinon . stub ( fs , 'readFile' ) . resolves ( 'Alice\nBob\nCharlie' ) ;
36
+ this . app = new Application ( ) ;
37
+ await this . app . getNames ( ) ;
36
38
} ) ;
37
39
38
40
afterEach ( ( ) => {
39
41
fs . readFile . restore ( ) ;
40
42
} ) ;
41
43
42
44
it ( 'should initialize with people and selected lists' , async ( ) => {
43
- const app = new Application ( ) ;
44
- await app . getNames ( ) ;
45
- expect ( app . people ) . to . deep . equal ( [ 'Alice' , 'Bob' , 'Charlie' ] ) ;
46
- expect ( app . selected ) . to . deep . equal ( [ ] ) ;
45
+ expect ( this . app . people ) . to . deep . equal ( [ 'Alice' , 'Bob' , 'Charlie' ] ) ;
46
+ expect ( this . app . selected ) . to . deep . equal ( [ ] ) ;
47
47
} ) ;
48
48
49
- it ( 'should select a random person' , async ( ) => {
50
- const app = new Application ( ) ;
51
- await app . getNames ( ) ;
52
- const person = app . selectNextPerson ( ) ;
53
- expect ( app . people ) . to . include ( person ) ;
54
- expect ( app . selected ) . to . include ( person ) ;
49
+ it ( 'should select a random person' , ( ) => {
50
+ const person = this . app . selectNextPerson ( ) ;
51
+ expect ( this . app . people ) . to . include ( person ) ;
52
+ expect ( this . app . selected ) . to . include ( person ) ;
55
53
} ) ;
56
54
57
- it ( 'should notify all selected people' , async ( ) => {
58
- const app = new Application ( ) ;
59
- await app . getNames ( ) ;
60
- const mailSystemSpy = sinon . spy ( app . mailSystem , 'send' ) ;
55
+ it ( 'should notify all selected people' , ( ) => {
56
+ const mailSystemSpy = sinon . spy ( this . app . mailSystem , 'send' ) ;
61
57
62
- app . selectNextPerson ( ) ;
63
- app . selectNextPerson ( ) ;
64
- app . notifySelected ( ) ;
58
+ this . app . selectNextPerson ( ) ;
59
+ this . app . selectNextPerson ( ) ;
60
+ this . app . notifySelected ( ) ;
65
61
66
62
expect ( mailSystemSpy . calledTwice ) . to . be . true ;
67
63
} ) ;
0 commit comments