Skip to content

Commit ae38e6e

Browse files
authored
Update main_test.js
1 parent af27102 commit ae38e6e

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

lab2/main_test.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
const TestLab = require('node:test');
21
const Test = require('node:test');
32
const assert = require('assert');
43
const fileSystem = require('fs');
54

65
// 模擬 fileSystem 模組的 readFile 方法
7-
TestLab.mock.method(fileSystem, 'readFile', (file, options, callback) => {
86
Test.mock.method(fileSystem, 'readFile', (file, options, callback) => {
97
callback(null, 'ray\nnina\neric');
108
});
119

1210
const { Application, MailSystem } = require('./main');
1311

1412
// 測試 MailSystem 的 write 方法
15-
TestLab('MailSystem_write()', () => {
1613
Test('MailSystem_write()', () => {
1714
const mailSystem = new MailSystem();
1815
assert.strictEqual(mailSystem.write('ray'), 'Congrats, ray!');
@@ -21,20 +18,16 @@ Test('MailSystem_write()', () => {
2118
});
2219

2320
// 測試 MailSystem 的 send 方法
24-
TestLab('MailSystem_send()', () => {
2521
Test('MailSystem_send()', () => {
2622
const mailSystem = new MailSystem();
2723
const name = 'ray';
28-
TestLab.mock.method(Math, 'random', () => 0.7); // 成功概率高
2924
Test.mock.method(Math, 'random', () => 0.7);
3025
assert.strictEqual(mailSystem.send(name, 'success'), true);
31-
TestLab.mock.method(Math, 'random', () => 0.3); // 失敗概率高
3226
Test.mock.method(Math, 'random', () => 0.3);
3327
assert.strictEqual(mailSystem.send(name, 'fail'), false);
3428
});
3529

3630
// 測試 Application 的 getNames 方法
37-
TestLab('Application_getNames()', async () => {
3831
Test('Application_getNames()', async () => {
3932
const application = new Application();
4033
const nameList = ['ray', 'nina', 'eric'];
@@ -43,46 +36,41 @@ Test('Application_getNames()', async () => {
4336
});
4437

4538
// 測試 Application 的 getRandomPerson 方法
46-
TestLab('Application_getRandomPerson()', async () => {
4739
Test('Application_getRandomPerson()', async () => {
4840
const application = new Application();
4941
const names = await application.getNames();
50-
TestLab.mock.method(Math, 'random', () => 0);
5142
Test.mock.method(Math, 'random', () => 0);
5243
assert.strictEqual(application.getRandomPerson(), 'ray');
53-
TestLab.mock.method(Math, 'random', () => 0.4);
5444
Test.mock.method(Math, 'random', () => 0.4);
5545
assert.strictEqual(application.getRandomPerson(), 'nina');
56-
TestLab.mock.method(Math, 'random', () => 0.7);
5746
Test.mock.method(Math, 'random', () => 0.7);
5847
assert.strictEqual(application.getRandomPerson(), 'eric');
5948
});
6049

6150
// 測試 Application 的 selectNextPerson 方法
62-
TestLab('Application_selectNextPerson()', async () => {
6351
Test('Application_selectNextPerson()', async () => {
6452
const application = new Application();
6553
const names = await application.getNames();
6654
application.selected = ['ray'];
6755
let counter = 0;
68-
TestLab.mock.method(application, 'getRandomPerson', () => {
6956
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);
7464
});
7565

7666
// 測試 Application 的 notifySelected 方法
77-
TestLab('Application_notifySelected()', async () => {
7867
Test('Application_notifySelected()', async () => {
7968
const application = new Application();
8069
application.people = ['ray', 'nina', 'eric'];
8170
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);
8471
application.mailSystem.send = Test.mock.fn(application.mailSystem.send);
8572
application.mailSystem.write = Test.mock.fn(application.mailSystem.write);
8673
application.notifySelected();
8774
assert.strictEqual(application.mailSystem.send.mock.calls.length, 3);
8875
assert.strictEqual(application.mailSystem.write.mock.calls.length, 3);
76+
});

0 commit comments

Comments
 (0)