【问题标题】:Cant clear database after tests are run on Mocha and Chai using mongodb with mongoose in my node.js app在我的 node.js 应用程序中使用 mongodb 和 mongoose 在 Mocha 和 Chai 上运行测试后无法清除数据库
【发布时间】: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


    【解决方案1】:

    我认为模型没有任何内置的 drop 方法。您需要使用需要处理当前数据库的db.collection("collectionName").drop() 或使用deleteMany 并删除集合中的所有内容。像这样的

    describe('Task', (done) => {
      afterEach(() => {
        Task.crud.deleteMany({}, (err, response) => {
          done();
        });
      });
    });
    
    

    【讨论】:

    • 对不起,但它仍然无法正常工作,同样已经尝试过。
    • 您的第一个描述没有任何测试。您可以尝试将所有描述移到父描述中并在其中放置 afterEach 吗?还值得在 ​​afterEach 中设置一个断点以查看它是否正在执行?
    猜你喜欢
    • 2021-07-01
    • 2018-02-27
    • 1970-01-01
    • 2017-06-09
    • 2020-10-30
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    相关资源
    最近更新 更多