Skip to content

[LAB1] 511558015 #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
const test = require('node:test');
const assert = require('assert');
const { MyClass, Student } = require('./main');
//建立 MyClass及Student

//test student
test("Test MyClass's addStudent", () => {
// TODO
throw new Error("Test not implemented");
});
const myClass = new MyClass();
const student = new Student();

test("Test MyClass's getStudentById", () => {
// TODO
throw new Error("Test not implemented");
// add stduent return 0
assert.strictEqual(myClass.addStudent(student), 0);

// add no stduent return -1
assert.strictEqual(myClass.addStudent("not a student"), -1);
});
//test get studentID
test("Test MyClass's getStudentById", () => {
const myClass = new MyClass();
const student = new Student();
myClass.addStudent(student);

test("Test Student's setName", () => {
// TODO
throw new Error("Test not implemented");
// get studentID 0
assert.strictEqual(myClass.getStudentById(0), student);

// Test get studentID -1 and 100 return null
assert.strictEqual(myClass.getStudentById(-1), null);
assert.strictEqual(myClass.getStudentById(100), null);
});
// add student Name A
test("Test Student's setName", () => {
const student = new Student();

// add student A test A
student.setName("A");
assert.strictEqual(student.getName(), "A");

// add student 123 test A
student.setName(123);
assert.strictEqual(student.getName(), "A");
});
// add student Name
test("Test Student's getName", () => {
// TODO
throw new Error("Test not implemented");
});
const student = new Student();

// NO add student return
assert.strictEqual(student.getName(), '');

// add student B test B
student.setName("B");
assert.strictEqual(student.getName(), "B");
});

29 changes: 27 additions & 2 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
const test = require('node:test');
const assert = require('assert');
const { Application, MailSystem } = require('./main');
const sinon = require('sinon');

test('notifySelected sends mail to all selected people', async () => {
const mockMailSystem = sinon.createStubInstance(MailSystem);
mockMailSystem.send.returns(true); // 假設發送郵件總是成功

const app = new Application();
app.mailSystem = mockMailSystem; // 使用模擬的 MailSystem

// 假設 getNames 方法返回特定的名單
app.getNames = async () => {
return [['Alice', 'Bob', 'Charlie'], ['Alice']];
};

await app.getNames();
app.selectNextPerson(); // 選擇下一個人(假設為 Bob)
app.notifySelected();

// 驗證 send 方法是否被調用了正確的次數(應為已選擇人數)
assert.strictEqual(mockMailSystem.send.callCount, app.selected.length);

// 確保 send 方法被正確的參數調用
app.selected.forEach((person) => {
assert(mockMailSystem.send.calledWith(person, `Congrats, ${person}!`));
});
});


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