Skip to content

Commit 768790b

Browse files
authored
Update main_test.js
1 parent eaafdad commit 768790b

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

lab2/main_test.js

+23-25
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@ const writeFile = util.promisify(fs.writeFile);
77
const { Application, MailSystem } = require('./main');
88
const path = require('node:path');
99

10-
// TODO: write your tests here
11-
// Remember to use Stub, Mock, and Spy when necessary
12-
// Remember to use Stub, Mock, and Spy when necessary
10+
// Mock fs.unlinkSync
11+
const originalUnlinkSync = fs.unlinkSync;
12+
fs.unlinkSync = (path) => {
13+
console.log(`Mocked unlinkSync called with path: ${path}`);
14+
};
15+
16+
// Mock fs.readFileSync
17+
const originalReadFileSync = fs.readFileSync;
18+
fs.readFileSync = (path, encoding) => {
19+
console.log(`Mocked readFileSync called with path: ${path} and encoding: ${encoding}`);
20+
return "JJ\nEE\nRR\nYY";
21+
};
1322

1423
test("Test MailSystem's write", () => {
1524
const ms = new MailSystem();
@@ -19,7 +28,8 @@ test("Test MailSystem's write", () => {
1928

2029
test("Test MailSystem's send", () => {
2130
const ms = new MailSystem();
22-
const orgrdm = Math.random;
31+
const originalMathRandom = Math.random;
32+
2333
Math.random = () => 0.4;
2434
let context = ms.send("Jerry", "Test Message");
2535
assert.strictEqual(context, false);
@@ -28,7 +38,7 @@ test("Test MailSystem's send", () => {
2838
context = ms.send("Jerry", "Test Message");
2939
assert.strictEqual(context, true);
3040

31-
Math.random = orgrdm;
41+
Math.random = originalMathRandom;
3242
});
3343

3444
test("Test Application's getNames", async () => {
@@ -38,12 +48,11 @@ test("Test Application's getNames", async () => {
3848

3949
const app = new Application();
4050

41-
let ctx = new Array([],[]);
42-
ctx = await app.getNames();
51+
let ctx = await app.getNames();
4352
assert.deepStrictEqual(ctx[0], ["JJ", "EE", "RR", "YY"]);
4453
assert.deepStrictEqual(ctx[1], []);
4554

46-
fs.unlinkSync(fn_);
55+
originalUnlinkSync(fn_);
4756
});
4857

4958
test("Test Application's getRandomPerson", async () => {
@@ -53,8 +62,7 @@ test("Test Application's getRandomPerson", async () => {
5362

5463
const app = new Application();
5564

56-
let ctx = new Array([],[]);
57-
ctx = await app.getNames();
65+
let ctx = await app.getNames();
5866
assert.deepStrictEqual(ctx[0], ["JJ", "EE"]);
5967
assert.deepStrictEqual(ctx[1], []);
6068

@@ -64,7 +72,7 @@ test("Test Application's getRandomPerson", async () => {
6472
rdmPeople = await app.getRandomPerson();
6573
assert.ok(app.people.includes(rdmPeople));
6674

67-
fs.unlinkSync(fn_);
75+
originalUnlinkSync(fn_);
6876
});
6977

7078
test("Test Application's selectNextPerson", async () => {
@@ -73,8 +81,7 @@ test("Test Application's selectNextPerson", async () => {
7381
await writeFile(fn_, data_, "utf-8");
7482

7583
const app = new Application();
76-
let ctx = new Array([],[]);
77-
ctx = await app.getNames();
84+
let ctx = await app.getNames();
7885
assert.deepStrictEqual(ctx[0], ["JJ", "EE"]);
7986
assert.deepStrictEqual(ctx[1], []);
8087

@@ -98,7 +105,6 @@ test("Test Application's selectNextPerson", async () => {
98105
app.getRandomPerson = orgsnp;
99106
rdperson = app.selectNextPerson();
100107
assert.strictEqual(rdperson, null);
101-
assert.strictEqual(rdperson, null);
102108
assert.strictEqual(app.selected.length, 2);
103109

104110
let cnt = 0;
@@ -113,18 +119,16 @@ test("Test Application's selectNextPerson", async () => {
113119
rdperson = app.selectNextPerson();
114120
assert.strictEqual(rdperson, 'EE');
115121
assert.deepStrictEqual(app.selected, ['JJ', 'EE']);
116-
fs.unlinkSync(fn_);
122+
originalUnlinkSync(fn_);
117123
});
118124

119125
test("Test Application's notifySelected", async () => {
120-
121126
const fn_ = 'name_list.txt';
122127
const data_ = "JJ\nEE";
123128
await writeFile(fn_, data_, "utf-8");
124129

125130
const app = new Application();
126-
let ctx = new Array([],[]);
127-
ctx = await app.getNames();
131+
let ctx = await app.getNames();
128132
assert.deepStrictEqual(ctx[0], ["JJ", "EE"]);
129133
assert.deepStrictEqual(ctx[1], []);
130134

@@ -155,11 +159,5 @@ test("Test Application's notifySelected", async () => {
155159
assert.deepStrictEqual(call.arguments, ["EE"]);
156160
assert.deepStrictEqual(call.result, "Congrats, EE!");
157161
assert.deepStrictEqual(call.error, undefined);
158-
fs.unlinkSync(fn_);
162+
originalUnlinkSync(fn_);
159163
});
160-
161-
const { Application, MailSystem } = require('./main');
162-
163-
// TODO: write your tests here
164-
// Remember to use Stub, Mock, and Spy when necessary
165-

0 commit comments

Comments
 (0)