首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >钩子之前的Mocha不能与chai-http一起工作

钩子之前的Mocha不能与chai-http一起工作
EN

Stack Overflow用户
提问于 2018-07-15 21:03:03
回答 1查看 1.3K关注 0票数 0

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

代码语言:javascript
复制
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();
    });
  });
});

EN

回答 1

Stack Overflow用户

发布于 2018-07-15 22:09:30

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

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

它应该是:

代码语言:javascript
复制
describe('Empty User Collection before test', function () {
  before(function () {
    return User.collection.drop();
  });

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

代码语言:javascript
复制
  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 -或者所有测试都可以具有带有before的父describe

代码语言:javascript
复制
describe('Suite', function () {
  before(function () {
    return User.collection.drop();
  });

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

  describe('Testing /login end point', () => {...})
  ...
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51348651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档