Skip to content

Commit 98bfae1

Browse files
authored
Update main_test.js
1 parent 96837e8 commit 98bfae1

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

lab2/main_test.js

+17-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { expect } = require('chai');
22
const sinon = require('sinon');
3-
const fs = require('fs');
3+
const fs = require('fs').promises;
44
const { MailSystem, Application } = require('./main_test');
55

66
describe('MailSystem', () => {
@@ -16,13 +16,13 @@ describe('MailSystem', () => {
1616
const name = 'Alice';
1717
const context = mailSystem.write(name);
1818

19-
sinon.stub(Math, 'random').returns(0.6);
19+
sinon.stub(Math, 'random').returns(0.6); // 假設成功
2020
let result = mailSystem.send(name, context);
2121
expect(result).to.be.true;
2222

2323
Math.random.restore();
2424

25-
sinon.stub(Math, 'random').returns(0.4);
25+
sinon.stub(Math, 'random').returns(0.4); // 假設失敗
2626
result = mailSystem.send(name, context);
2727
expect(result).to.be.false;
2828

@@ -31,37 +31,33 @@ describe('MailSystem', () => {
3131
});
3232

3333
describe('Application', () => {
34-
beforeEach(() => {
34+
beforeEach(async () => {
3535
sinon.stub(fs, 'readFile').resolves('Alice\nBob\nCharlie');
36+
this.app = new Application();
37+
await this.app.getNames();
3638
});
3739

3840
afterEach(() => {
3941
fs.readFile.restore();
4042
});
4143

4244
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([]);
4747
});
4848

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);
5553
});
5654

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');
6157

62-
app.selectNextPerson();
63-
app.selectNextPerson();
64-
app.notifySelected();
58+
this.app.selectNextPerson();
59+
this.app.selectNextPerson();
60+
this.app.notifySelected();
6561

6662
expect(mailSystemSpy.calledTwice).to.be.true;
6763
});

0 commit comments

Comments
 (0)