【问题标题】:Mocha before hook is not working with chai-http钩子前的摩卡不适用于 chai-http
【发布时间】:2018-07-15 13:03:02
【问题描述】:

这是我的测试代码。我正在测试一个 API。问题是“after”钩子正在工作并在测试结束后删除数据库。但是“之前”钩子不起作用。这里有什么问题?我试过但无法找出问题所在。我试图只用一个虚拟测试来运行 before 钩子,比如在控制台中记录一些东西。也没用。

const chai = require('chai');
const { assert } = require('chai');
const chaiHttp = require('chai-http');
const mongoose = require('mongoose');

mongoose.Promise = global.Promise;
require('../resolvedir');
const User = require('models/User');
const server = require('bin/www');
const testData = require('./test_data');

chai.use(chaiHttp);

describe('Empty User Collection before test', function () {
  it('should drop User Collection before test starts', function () {
    before(function (done) {
      User.collection.drop();
      done();
    });
  });
});


describe('Testing /registration end point', () => {
  it('should return a valid JWT token', (done) => {
    chai.request(server)
      .post('/register')
      .send({ name: testData.name, email: testData.email, password: testData.password })
      .end((err, res) => {
        assert.equal(res.status, 200, 'Http response code is 200');
        assert.exists(res.body.auth, 'Auth confirmation message exist');
        assert.isTrue(res.body.auth, 'Auth confirmation message is true');
        assert.exists(res.body.token, 'JWT token is neither null or undefined');
        assert.isString(res.body.token, 'JWT token is string');
        done();
      });
  });

  it('should fail registration', (done) => {
    chai.request(server)
      .post('/register')
      .send(testData)
      .end((err, res) => {
        assert.equal(res.status, 409, 'Http response code is 409');
        assert.isString(res.body.message);
        assert.equal(res.body.message, 'User Exist');
        done();
      });
  });
});

describe('Testing /login end point', function () {
  it('should get a valid JWT token on successful login', function (done) {
    chai.request(server)
      .post('/login')
      .send({ email: testData.email, password: testData.password })
      .end((err, res) => {
        assert.isString(res.body.token, 'JWT token is string');
        done();
      });
  });
});

describe('Empty User Collection after test', function () {
  it('should drop User Collection after test ends', function () {
    after(function (done) {
      User.collection.drop();
      done();
    });
  });
});

【问题讨论】:

  • 我想你在找什么mochajs.org/#root-level-hooks
  • 目前还不清楚究竟是什么不起作用。 before 只使用一次,在使用 chai-http 的测试中不使用。发布的代码有几个问题,我已经解决了。

标签: javascript node.js mocha.js chai chai-http


【解决方案1】:

Mongoose 和 MongoDB 支持承诺。 chai-http supports promises 也是。

before 应该位于 describe 块内,而不是位于 it 内。 Mocha 支持异步块的承诺,不需要done。但是测试使用done 并且方式不一致。 before 是异步的,但 done() 是同步调用的。 done() 仅在测试成功时调用,当断言失败时会导致测试超时。

应该是:

describe('Empty User Collection before test', function () {
  before(function () {
    return User.collection.drop();
  });

  it('should drop User Collection before test starts', function () {
    ...
  });
});

还有

  it('should get a valid JWT token on successful login', function () {
    return chai.request(server)
      .post('/login')
      .send({ email: testData.email, password: testData.password })
      .then((res) => {
        assert.isString(res.body.token, 'JWT token is string');
      });
  });

等等

如果应该在 Testing /registration end point 测试套件中删除数据库,则应该有 before 删除数据库 - 或者所有测试都可以有父 describebefore

describe('Suite', function () {
  before(function () {
    return User.collection.drop();
  });

  describe('Testing /registration end point', () => {...})

  describe('Testing /login end point', () => {...})
  ...
});

【讨论】:

  • 我尝试了您的最后一个解决方案。创建了一个父描述块并随后调用了before。但仍然没有运气。
猜你喜欢
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-27
  • 2018-02-21
  • 1970-01-01
相关资源
最近更新 更多