You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.9 KiB
49 lines
1.9 KiB
const chai = require('chai');
|
|
const chaiHttp = require('chai-http');
|
|
const app = require('../server'); // 引入 Express 应用
|
|
const pool = require('../db'); // 引入数据库连接池
|
|
const expect = chai.expect;
|
|
|
|
chai.use(chaiHttp);
|
|
|
|
describe('Student Score API Tests', () => {
|
|
// 在测试开始前准备一些测试数据
|
|
before(async () => {
|
|
const connection = await pool.getConnection();
|
|
await connection.query('INSERT INTO students (student_id, student_name, score, call_count) VALUES (123456, "HE", 85, 0)');
|
|
await connection.query('INSERT INTO students (student_id, student_name, score, call_count) VALUES (412536, "dfe", 90, 0)');
|
|
connection.release();
|
|
});
|
|
|
|
// 测试结束后清理数据
|
|
after(async () => {
|
|
const connection = await pool.getConnection();
|
|
await connection.query('DELETE FROM students WHERE student_id IN (123456, 412536)');
|
|
connection.release();
|
|
});
|
|
|
|
it('should return score of an existing student', (done) => {
|
|
chai.request(app)
|
|
.post('/api/get-score')
|
|
.send({ student_id: 123456 })
|
|
.end((err, res) => {
|
|
expect(res).to.have.status(200);
|
|
expect(res.body).to.be.an('object');
|
|
expect(res.body).to.have.property('student_id', 123456);
|
|
expect(Number(res.body.score)).to.equal(85); // 将 score 转为数值进行比较
|
|
done();
|
|
});
|
|
});
|
|
it('should return 404 for a non-existing student', (done) => {
|
|
chai.request(app)
|
|
.post('/api/get-score')
|
|
.send({ student_id: 999 }) // 使用不存在的 ID
|
|
.end((err, res) => {
|
|
expect(res).to.have.status(404);
|
|
expect(res.body).to.be.an('object');
|
|
expect(res.body).to.have.property('message', '学生不存在');
|
|
done();
|
|
});
|
|
});
|
|
});
|