diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..05f879c3 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -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"); -}); \ No newline at end of file + 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"); +}); + diff --git a/lab2/main_test.js b/lab2/main_test.js index 5034468e..b5bd9a03 100644 --- a/lab2/main_test.js +++ b/lab2/main_test.js @@ -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 \ No newline at end of file