1
1
const test = require ( 'node:test' ) ;
2
2
const assert = require ( 'assert' ) ;
3
+ const { mock } = require ( 'node:test' ) ; // 使用 node:test 內建 Mock
3
4
const { Application, MailSystem } = require ( './main' ) ;
4
5
5
- // TODO: write your tests here
6
- // Remember to use Stub, Mock, and Spy when necessary
6
+ test ( "Test MailSystem's write method" , ( ) => {
7
+ const mailSystem = new MailSystem ( ) ;
8
+ const result = mailSystem . write ( "Alice" ) ;
9
+ assert . strictEqual ( result , "Congrats, Alice!" ) ;
10
+ } ) ;
11
+
12
+ test ( "Test MailSystem's send method with Stub" , ( ) => {
13
+ const mailSystem = new MailSystem ( ) ;
14
+
15
+ // 使用 node:test 內建 mock 來 Stub `Math.random`
16
+ mock . method ( global . Math , "random" , ( ) => 0.6 ) ;
17
+ assert . strictEqual ( mailSystem . send ( "Bob" , "Congrats, Bob!" ) , true ) ;
18
+
19
+ mock . method ( global . Math , "random" , ( ) => 0.4 ) ;
20
+ assert . strictEqual ( mailSystem . send ( "Bob" , "Congrats, Bob!" ) , false ) ;
21
+ } ) ;
22
+
23
+ test ( "Test Application's getNames method with Stub" , async ( ) => {
24
+ // 先 Stub `Application.prototype.getNames()`,避免 `constructor()` 內部讀取檔案
25
+ mock . method ( Application . prototype , 'getNames' , async ( ) => [ [ "Alice" , "Bob" , "Charlie" ] , [ ] ] ) ;
26
+
27
+ // **現在才建立 `Application` 物件**
28
+ const app = new Application ( ) ;
29
+
30
+ // 呼叫 `getNames()`,確保它回傳 Stub 值
31
+ const [ people , selected ] = await app . getNames ( ) ;
32
+
33
+ assert . deepStrictEqual ( people , [ "Alice" , "Bob" , "Charlie" ] ) ;
34
+ assert . deepStrictEqual ( selected , [ ] ) ;
35
+ } ) ;
36
+
37
+ test ( "Test Application's selectNextPerson with Spy" , ( ) => {
38
+ // 先 Stub `getNames()` 避免 `constructor()` 內部讀取檔案
39
+ mock . method ( Application . prototype , 'getNames' , async ( ) => [ [ "Alice" , "Bob" , "Charlie" ] , [ ] ] ) ;
40
+
41
+ // 創建 `Application` 實例
42
+ const app = new Application ( ) ;
43
+
44
+ // 手動設置 `people` 和 `selected`
45
+ app . people = [ "Alice" , "Bob" , "Charlie" ] ;
46
+ app . selected = [ ] ;
47
+
48
+ // Spy `getRandomPerson`
49
+ const spy = mock . method ( app , "getRandomPerson" , ( ) => "Bob" ) ;
50
+
51
+ // 測試 `selectNextPerson()`
52
+ const person1 = app . selectNextPerson ( ) ;
53
+ assert . ok ( spy . mock . calls . length > 0 ) ; // 確保 `getRandomPerson()` 被呼叫
54
+ assert . ok ( app . selected . includes ( "Bob" ) ) ;
55
+ } ) ;
56
+
57
+ test ( "Test Application's notifySelected using Mock" , ( ) => {
58
+ // 先 Stub `getNames()`,避免 `constructor()` 內部讀取 `name_list.txt`
59
+ mock . method ( Application . prototype , 'getNames' , async ( ) => [ [ "Alice" , "Bob" ] , [ ] ] ) ;
60
+
61
+ // 創建 `Application` 物件
62
+ const app = new Application ( ) ;
63
+
64
+ // 手動設定 `selected`
65
+ app . selected = [ "Alice" , "Bob" ] ;
66
+
67
+ // Mock `MailSystem.write()` 和 `send()`
68
+ const mockMailSystem = mock . method ( app . mailSystem , "write" , ( name ) => "Mocked content" ) ;
69
+ const mockSend = mock . method ( app . mailSystem , "send" , ( name , content ) => true ) ;
70
+
71
+ // 測試 `notifySelected()`
72
+ app . notifySelected ( ) ;
73
+
74
+ // 確保 Mock 方法被正確呼叫
75
+ assert . ok ( mockMailSystem . mock . calls . length === 2 ) ;
76
+ assert . ok ( mockSend . mock . calls . length === 2 ) ;
77
+ } ) ;
0 commit comments