Skip to content

Commit edd9733

Browse files
authored
Merge pull request #19 from ej04520yjo4/511558015
[LAB1] 511558015
2 parents dfaa7dc + cc340f5 commit edd9733

File tree

2 files changed

+69
-14
lines changed

2 files changed

+69
-14
lines changed

lab1/main_test.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,53 @@
11
const test = require('node:test');
22
const assert = require('assert');
33
const { MyClass, Student } = require('./main');
4+
//建立 MyClass及Student
45

6+
//test student
57
test("Test MyClass's addStudent", () => {
6-
// TODO
7-
throw new Error("Test not implemented");
8-
});
8+
const myClass = new MyClass();
9+
const student = new Student();
910

10-
test("Test MyClass's getStudentById", () => {
11-
// TODO
12-
throw new Error("Test not implemented");
11+
// add stduent return 0
12+
assert.strictEqual(myClass.addStudent(student), 0);
13+
14+
// add no stduent return -1
15+
assert.strictEqual(myClass.addStudent("not a student"), -1);
1316
});
17+
//test get studentID
18+
test("Test MyClass's getStudentById", () => {
19+
const myClass = new MyClass();
20+
const student = new Student();
21+
myClass.addStudent(student);
1422

15-
test("Test Student's setName", () => {
16-
// TODO
17-
throw new Error("Test not implemented");
23+
// get studentID 0
24+
assert.strictEqual(myClass.getStudentById(0), student);
25+
26+
// Test get studentID -1 and 100 return null
27+
assert.strictEqual(myClass.getStudentById(-1), null);
28+
assert.strictEqual(myClass.getStudentById(100), null);
1829
});
30+
// add student Name A
31+
test("Test Student's setName", () => {
32+
const student = new Student();
1933

34+
// add student A test A
35+
student.setName("A");
36+
assert.strictEqual(student.getName(), "A");
37+
38+
// add student 123 test A
39+
student.setName(123);
40+
assert.strictEqual(student.getName(), "A");
41+
});
42+
// add student Name
2043
test("Test Student's getName", () => {
21-
// TODO
22-
throw new Error("Test not implemented");
23-
});
44+
const student = new Student();
45+
46+
// NO add student return
47+
assert.strictEqual(student.getName(), '');
48+
49+
// add student B test B
50+
student.setName("B");
51+
assert.strictEqual(student.getName(), "B");
52+
});
53+

lab2/main_test.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
const test = require('node:test');
22
const assert = require('assert');
33
const { Application, MailSystem } = require('./main');
4+
const sinon = require('sinon');
5+
6+
test('notifySelected sends mail to all selected people', async () => {
7+
const mockMailSystem = sinon.createStubInstance(MailSystem);
8+
mockMailSystem.send.returns(true); // 假設發送郵件總是成功
9+
10+
const app = new Application();
11+
app.mailSystem = mockMailSystem; // 使用模擬的 MailSystem
12+
13+
// 假設 getNames 方法返回特定的名單
14+
app.getNames = async () => {
15+
return [['Alice', 'Bob', 'Charlie'], ['Alice']];
16+
};
17+
18+
await app.getNames();
19+
app.selectNextPerson(); // 選擇下一個人(假設為 Bob)
20+
app.notifySelected();
21+
22+
// 驗證 send 方法是否被調用了正確的次數(應為已選擇人數)
23+
assert.strictEqual(mockMailSystem.send.callCount, app.selected.length);
24+
25+
// 確保 send 方法被正確的參數調用
26+
app.selected.forEach((person) => {
27+
assert(mockMailSystem.send.calledWith(person, `Congrats, ${person}!`));
28+
});
29+
});
30+
431

5-
// TODO: write your tests here
6-
// Remember to use Stub, Mock, and Spy when necessary

0 commit comments

Comments
 (0)