【发布时间】:2020-05-08 08:40:28
【问题描述】:
我将包含相同所需的两个文件 主要问题是,每次运行测试后,我都不想在我的数据库中添加更多条目,并且应该在测试完成后对其进行清理 所以我使用了 aftereach() 但我想它不起作用是因为我做错了什么,你能帮我解决这个问题吗?
这是我的 test.js
process.env.NODE_ENV = 'test';
// const mongoose = require('mongoose');
const chai = require('chai');
const chaiHttp = require('chai-http');
const Task = require('../config/model');
const server = require('../index');
const { expect } = chai;
chai.use(chaiHttp);
describe('Task', (done) => {
afterEach(() => {
Task.crud.drop();
done();
});
});
// Test Get Tasks
describe('/GET tasks', () => {
it('it should GET all the tasks', async () => {
const res = await chai.request(server)
.get('/task');
// .end((err, res) => {
expect(res).to.have.status(200);
// expect(res.body).to.be.a('array');
// done(err);
});
});
describe('/Post tasks', () => {
it('should Post the task', async () => {
const taskPost = {
task: 'run as fast as possible you idiot',
};
const res = await chai.request(server)
.post('/task')
.send(taskPost);
// .end((err, res) => {
expect(res).to.have.status(200);
// done();
});
});
describe('/GET/:ID', () => {
it('should Get the task by ID', async () => {
const tasks = new Task({ task: 'The Lord of the Rings' });
const task = await tasks.save();
const res = await chai.request(server)
.get(`/task/${task.id}`)
.send(task);
// .end((error, res) => {
expect(res).to.have.status(200);
// done();
// });
});
});
describe('/PUT/:ID task', () => {
it('it should UPDATE a task given the id', async () => {
const tasks = new Task({ task: 'The Chronicles of Narnia' });
const task = await tasks.save();
const res = await chai.request(server)
.put(`/task/${task.id}`)
.send({ task: 'The Chronicles of Sarvesh' });
// .end((error, res) => {
expect(res).to.have.status(200);
// });
});
});
以及 /config/model 中的文件
const mongoose = require('mongoose');
const Schema = new mongoose.Schema({
task: {
type: String,
required: true,
},
});
module.exports = mongoose.model('crud', Schema, 'crud');
【问题讨论】:
标签: node.js testing mongoose mocha.js chai