diff --git a/lab1/README.md b/lab1/README.md index 5b2779a5..df0ab9f5 100644 --- a/lab1/README.md +++ b/lab1/README.md @@ -2,7 +2,7 @@ ## Introduction -In this lab, you will write unit tests for functions implemented in `main.js`. You can learn how to use classes and functions in it by uncommenting the code in `main.js.` (But remember don't commit them on GitHub) +In this lab, you will write unit tests for functions implemented in `main.js`. You can learn how to use classes and functions in it by uncommenting the code in it. (But remember don't commit them on GitHub) ## Requirement diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..4b7aa1b1 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -2,22 +2,31 @@ const test = require('node:test'); const assert = require('assert'); const { MyClass, Student } = require('./main'); -test("Test MyClass's addStudent", () => { - // TODO - throw new Error("Test not implemented"); +test("Test MyClass's addStudent", async (t) => { + const myClass = new MyClass(); + const student = new Student(); + student.setName("王小明"); + const index = myClass.addStudent(student); + assert.strictEqual(index, 0); // 增加學生王小明有成功的話,return index = 0 }); -test("Test MyClass's getStudentById", () => { - // TODO - throw new Error("Test not implemented"); +test("Test MyClass's getStudentById", async (t) => { + const myClass = new MyClass(); + const student = new Student(); + student.setName("王小明"); + myClass.addStudent(student); + const retrievedStudent = myClass.getStudentById(0); + assert.strictEqual(retrievedStudent.getName(), "王小明"); // 是否能在 index = 0,找到王小明 }); -test("Test Student's setName", () => { - // TODO - throw new Error("Test not implemented"); +test("Test Student's setName", async (t) => { + const student = new Student(); + student.setName("王小明"); + assert.strictEqual(student.name, "王小明"); // setName 有沒有在 student 物件成功附加上王小明,這個 name 屬性 }); -test("Test Student's getName", () => { - // TODO - throw new Error("Test not implemented"); -}); \ No newline at end of file +test("Test Student's getName", async (t) => { + const student = new Student(); + student.setName("王小明"); + assert.strictEqual(student.getName(), "王小明"); // getName 方法是否正確的 return 王小明這個名字 +});