Skip to content

Commit a39ebdf

Browse files
authored
Update main_test.js
1 parent 053accb commit a39ebdf

File tree

1 file changed

+52
-64
lines changed

1 file changed

+52
-64
lines changed

lab2/main_test.js

+52-64
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,68 @@
11
const test = require('node:test');
22
const assert = require('assert');
33
const fs = require('fs');
4-
const { Application, MailSystem } = require('./main');
5-
const sinon = require('sinon');
6-
7-
// Mock fs.readFile
8-
sinon.stub(fs, 'readFile').callsFake((file, options, callback) => {
9-
callback(null, 'Guan\nChen\nGala\n');
4+
test.mock.method(fs, 'readFile', (file, options, callback) => {
5+
callback(null, 'Guan\nChen\nGala');
106
});
7+
const { Application, MailSystem } = require('./main');
118

12-
test("MailSystem write", () => {
13-
const mailSystem = new MailSystem();
14-
assert.strictEqual(mailSystem.write("Guan"), "Congrats, Guan!");
9+
// TODO: write your tests here
10+
// Remember to use Stub, Mock, and Spy when necessary
11+
test("Test MailSystem's write", () => {
12+
const myClass = new MailSystem()
13+
assert.strictEqual( myClass.write("Guan"),"Congrats, Guan!")
1514
});
1615

17-
test("MailSystem send", () => {
18-
const mailSystem = new MailSystem();
19-
const originalMathRandom = Math.random;
20-
21-
Math.random = () => 0.6;
22-
const context = mailSystem.write("Guan");
23-
assert.strictEqual(mailSystem.send("Guan", context), true);
24-
25-
Math.random = () => 0.4;
26-
assert.strictEqual(mailSystem.send("Chen", context), false);
27-
28-
Math.random = originalMathRandom;
16+
test("Test MailSystem's send(name, context)", () => {
17+
Math.random = () => 0.6
18+
const mailSystem = new MailSystem()
19+
const context = mailSystem.write("Guan")
20+
const success = mailSystem.send("Guan",context)
21+
assert.strictEqual(success,true)
22+
Math.random = () => 0.5
23+
assert.strictEqual(mailSystem.send("Chen",context),false)
2924
});
3025

31-
test("Application getNames", async () => {
32-
const application = new Application();
33-
const [people, selected] = await application.getNames();
34-
// Remove any empty strings from the array
35-
assert.deepStrictEqual(people.filter(name => name), ["Guan", "Chen", "Gala"]);
36-
assert.deepStrictEqual(selected, []);
26+
test("Test Application's getNames", async () => {
27+
const application = new Application
28+
const [people, selected] = await application.getNames()
29+
assert.deepStrictEqual(people, ["Guan", "Chen", "Gala"])
30+
assert.deepStrictEqual(selected, [])
3731
});
3832

39-
test("Application getRandomPerson", () => {
40-
const application = new Application();
41-
application.people = ["Guan", "Chen", "Gala"];
42-
Math.random = () => 0;
43-
assert.deepStrictEqual(application.getRandomPerson(), "Guan");
44-
Math.random = () => 0.6;
45-
assert.deepStrictEqual(application.getRandomPerson(), "Chen");
46-
Math.random = () => 0.9;
47-
assert.deepStrictEqual(application.getRandomPerson(), "Gala");
33+
test("Test Application's getRandomPerson()", (t) => {
34+
const application = new Application
35+
application.people = ["Guan", "Chen", "Gala"]
36+
Math.random = () => 0
37+
assert.deepStrictEqual(application.getRandomPerson(), "Guan");
38+
Math.random = () => 0.6
39+
assert.deepStrictEqual(application.getRandomPerson(), "Chen");
40+
Math.random = () => 0.9
41+
assert.deepStrictEqual(application.getRandomPerson(), "Gala");
4842
});
4943

50-
test("Application selectNextPerson", async () => {
51-
const application = new Application();
52-
await application.getNames();
53-
application.selected = ["Guan"];
54-
let count = 1;
55-
const getRandomPersonStub = sinon.stub(application, 'getRandomPerson').callsFake(() => {
56-
const name_list = application.people;
57-
if (count < name_list.length) {
58-
return name_list[count++];
59-
} else {
60-
return null;
61-
}
62-
});
63-
64-
assert.strictEqual(application.selectNextPerson(), "Chen");
65-
assert.strictEqual(application.selectNextPerson(), "Gala");
66-
assert.strictEqual(application.selectNextPerson(), null);
67-
68-
getRandomPersonStub.restore();
44+
test("Test Application's selectNextPerson", async() => {
45+
const application = new Application
46+
const name_list = await application.getNames();
47+
application.selected = ["Guan"]
48+
let count = 0;
49+
test.mock.method(application, 'getRandomPerson', () => {
50+
if (count <= name_list.length) {
51+
return name_list[0][count++];
52+
}
53+
});
54+
assert.strictEqual(application.selectNextPerson(), "Chen")
55+
assert.strictEqual(application.selectNextPerson(), "Gala")
56+
assert.strictEqual(application.selectNextPerson(), null)
6957
});
7058

71-
test("Application notifySelected", () => {
72-
const application = new Application();
73-
application.people = ["Guan", "Chen", "Gala"];
74-
application.selected = ["Guan", "Chen", "Gala"];
75-
sinon.spy(application.mailSystem, 'write');
76-
sinon.spy(application.mailSystem, 'send');
77-
application.notifySelected();
78-
assert.strictEqual(application.mailSystem.write.callCount, application.people.length);
79-
assert.strictEqual(application.mailSystem.send.callCount, application.people.length);
59+
test("Test Application's notifySelected() ", () => {
60+
const application = new Application
61+
application.people = ["Guan", "Chen", "Gala"]
62+
application.selected = ["Guan", "Chen", "Gala"]
63+
application.mailSystem.write = test.mock.fn(application.mailSystem.write)
64+
application.mailSystem.send = test.mock.fn(application.mailSystem.send)
65+
application.notifySelected()
66+
assert.strictEqual(application.mailSystem.send.mock.calls.length, application.people.length);
67+
assert.strictEqual(application.mailSystem.write.mock.calls.length, application.people.length);
8068
});

0 commit comments

Comments
 (0)