Skip to content

Commit 9902fde

Browse files
authored
Merge pull request #139 from Chun-Ching-Hsu/313554033
[LAB2] 313554033
2 parents 8c2461f + 9163f5a commit 9902fde

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

lab2/main_test.js

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,77 @@
11
const test = require('node:test');
22
const assert = require('assert');
3+
const { mock } = require('node:test'); // 使用 node:test 內建 Mock
34
const { Application, MailSystem } = require('./main');
45

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

Comments
 (0)