Skip to content

Commit 0499706

Browse files
authored
Update main_test.js
1 parent f0bcaef commit 0499706

File tree

1 file changed

+85
-3
lines changed

1 file changed

+85
-3
lines changed

lab2/main_test.js

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,88 @@
1-
const test = require('node:test');
1+
const TestLab = require('node:test');
2+
const Test = require('node:test');
23
const assert = require('assert');
4+
const fileSystem = require('fs');
5+
6+
// 模擬 fileSystem 模組的 readFile 方法
7+
TestLab.mock.method(fileSystem, 'readFile', (file, options, callback) => {
8+
Test.mock.method(fileSystem, 'readFile', (file, options, callback) => {
9+
callback(null, 'ray\nnina\neric');
10+
});
11+
312
const { Application, MailSystem } = require('./main');
413

5-
// TODO: write your tests here
6-
// Remember to use Stub, Mock, and Spy when necessary
14+
// 測試 MailSystem 的 write 方法
15+
TestLab('MailSystem_write()', () => {
16+
Test('MailSystem_write()', () => {
17+
const mailSystem = new MailSystem();
18+
assert.strictEqual(mailSystem.write('ray'), 'Congrats, ray!');
19+
assert.strictEqual(mailSystem.write(null), 'Congrats, null!');
20+
assert.strictEqual(mailSystem.write('3345678'), 'Congrats, 3345678!');
21+
});
22+
23+
// 測試 MailSystem 的 send 方法
24+
TestLab('MailSystem_send()', () => {
25+
Test('MailSystem_send()', () => {
26+
const mailSystem = new MailSystem();
27+
const name = 'ray';
28+
TestLab.mock.method(Math, 'random', () => 0.7); // 成功概率高
29+
Test.mock.method(Math, 'random', () => 0.7);
30+
assert.strictEqual(mailSystem.send(name, 'success'), true);
31+
TestLab.mock.method(Math, 'random', () => 0.3); // 失敗概率高
32+
Test.mock.method(Math, 'random', () => 0.3);
33+
assert.strictEqual(mailSystem.send(name, 'fail'), false);
34+
});
35+
36+
// 測試 Application 的 getNames 方法
37+
TestLab('Application_getNames()', async () => {
38+
Test('Application_getNames()', async () => {
39+
const application = new Application();
40+
const nameList = ['ray', 'nina', 'eric'];
41+
const names = await application.getNames();
42+
assert.deepStrictEqual(names, [nameList, []]);
43+
});
44+
45+
// 測試 Application 的 getRandomPerson 方法
46+
TestLab('Application_getRandomPerson()', async () => {
47+
Test('Application_getRandomPerson()', async () => {
48+
const application = new Application();
49+
const names = await application.getNames();
50+
TestLab.mock.method(Math, 'random', () => 0);
51+
Test.mock.method(Math, 'random', () => 0);
52+
assert.strictEqual(application.getRandomPerson(), 'ray');
53+
TestLab.mock.method(Math, 'random', () => 0.4);
54+
Test.mock.method(Math, 'random', () => 0.4);
55+
assert.strictEqual(application.getRandomPerson(), 'nina');
56+
TestLab.mock.method(Math, 'random', () => 0.7);
57+
Test.mock.method(Math, 'random', () => 0.7);
58+
assert.strictEqual(application.getRandomPerson(), 'eric');
59+
});
60+
61+
// 測試 Application 的 selectNextPerson 方法
62+
TestLab('Application_selectNextPerson()', async () => {
63+
Test('Application_selectNextPerson()', async () => {
64+
const application = new Application();
65+
const names = await application.getNames();
66+
application.selected = ['ray'];
67+
let counter = 0;
68+
TestLab.mock.method(application, 'getRandomPerson', () => {
69+
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 () => {
74+
});
75+
76+
// 測試 Application 的 notifySelected 方法
77+
TestLab('Application_notifySelected()', async () => {
78+
Test('Application_notifySelected()', async () => {
79+
const application = new Application();
80+
application.people = ['ray', 'nina', 'eric'];
81+
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+
application.mailSystem.send = Test.mock.fn(application.mailSystem.send);
85+
application.mailSystem.write = Test.mock.fn(application.mailSystem.write);
86+
application.notifySelected();
87+
assert.strictEqual(application.mailSystem.send.mock.calls.length, 3);
88+
assert.strictEqual(application.mailSystem.write.mock.calls.length, 3);

0 commit comments

Comments
 (0)