使用Bookshelf和MySQL测试项目时的随机超时

时间:2017-09-20 13:07:57

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

我第一次运行测试规范时,所有测试都通过了,但是如果我继续再次运行它会导致测试超时,检查外键值无效时是否返回错误。

这是我的代码或与Bookshelf甚至Mocha相关的已知问题的错误吗?

这是我的测试规范:

var _ = require('lodash'),
    ModelsTaskList = require('../../models/taskList'),
    TaskList = ModelsTaskList.TaskList,
    server = require('../../index'),
    chai = require('chai'),
    chaiHttp = require('chai-http'),
    should = chai.should(),
    faker = require('faker');

chai.use(chaiHttp);

describe('TaskLists controller', function() {

    describe('/POST tasklists', () => {
        it('should not POST a tasklist without a status', (done) => {
            var wrong_tasklist = _.clone(test_tasklist);
            wrong_tasklist.statusId = undefined;

            chai.request(server)
                .post('/api/tasklists')
                .send(wrong_tasklist)
                .end((err, res) => {
                    res.should.have.status(200);
                    res.body.should.be.an('object');
                    res.body.should.have.property('error');
                    done();
                });
        });

        it('should not POST a tasklist with an invalid status', (done) => {
            var wrong_tasklist = _.clone(test_tasklist);
            wrong_tasklist.statusId = 99;

            chai.request(server)
                .post('/api/tasklists')
                .send(wrong_tasklist)
                .end((err, res) => {
                    res.should.have.status(200);
                    res.body.should.be.an('object');
                    res.body.should.have.property('error');
                    done();
                });
        });

        it('should not POST a tasklist without a user ID', (done) => {
            var wrong_tasklist = _.clone(test_tasklist);
            wrong_tasklist.createdBy = undefined;

            chai.request(server)
                .post('/api/tasklists')
                .send(wrong_tasklist)
                .end((err, res) => {
                    res.should.have.status(200);
                    res.body.should.be.an('object');
                    res.body.should.have.property('error');
                    done();
                });
        });

        it('should not POST a tasklist with an invalid user ID', (done) => {
            var wrong_tasklist = _.clone(test_tasklist);
            wrong_tasklist.createdBy = 99;

            chai.request(server)
                .post('/api/tasklists')
                .send(wrong_tasklist)
                .end((err, res) => {
                    res.should.have.status(200);
                    res.body.should.be.an('object');
                    res.body.should.have.property('error');
                    done();
                });
        });

        it('should not POST a tasklist with an estimated end date before the start date', (done) => {
            var wrong_tasklist = _.clone(test_tasklist);
            wrong_tasklist.startDate = faker.date.recent(1);
            wrong_tasklist.estimatedEndDate = faker.date.recent(2);

            chai.request(server)
                .post('/api/tasklists')
                .send(wrong_tasklist)
                .end((err, res) => {
                    res.should.have.status(200);
                    res.body.should.be.an('object');
                    res.body.should.have.property('error');
                    done();
                });
        });
    })

});

我得到的第一个执行:

/ POST任务列表   ✓不应该在没有状态的情况下发布任务列表(49ms)   ✓不应该发布状态无效的任务列表   ✓不应在没有用户ID的情况下发布任务列表   ✓不应使用无效的用户ID发布任务列表   ✓不应在开始日期之前发布具有估计结束日期的任务列表

接下来的执行:

    /POST tasklists
      ✓ should not POST a tasklist without a status
      1) should not POST a tasklist with an invalid status
      ✓ should not POST a tasklist without a user ID
      2) should not POST a tasklist with an invalid user ID
      ✓ should not POST a tasklist with an estimated end date before the start date


  4 passing (8s)
  2 failing

  1) TaskLists controller /POST tasklists should not POST a tasklist with an invalid status:
     Error: Timeout of 4000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.


  2) TaskLists controller /POST tasklists should not POST a tasklist with an invalid user ID:
     Error: Timeout of 4000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

这是在POST到/ api / tasklists时执行的代码:

var ModelsTaskList = require('../models/taskList'),
    TaskList = ModelsTaskList.TaskList,
    moment = require('moment'),
    ctlr;

var handleError = function (res, message) {
    res.json({
        status: false,
        tasklist: null,
        error: message
    });
};

ctlr = {

    create: function (req, res) { 
        if (req.body.statusId === undefined) {
            handleError(res, 'ID do status não informado');
        } 
        else if (req.body.createdBy === undefined) {
            handleError(res, 'ID do usuário não informado');
        }
        else if (req.body.startDate !== undefined) {
            var startDate = moment(req.body.startDate);

            if (req.body.estimatedEndDate !== undefined) {
                if (moment(req.body.estimatedEndDate).isBefore(startDate)) {
                    handleError(res, 'Data estimada de conclusão deve ser maior que a data de início');
                }
            }
        }            
        else {
            TaskList.forge(req.body)
                .save({ method: 'insert', require: true })
                .then(result => {
                    res.json({
                        status: true,
                        tasklist: result,
                        error: null
                    });
                })
                .catch(err => {
                    handleError(res, err.message);
                });
        }

    },

    read: function (req, res) {

    }
};

module.exports = ctlr;

1 个答案:

答案 0 :(得分:0)

我的代码出错了。根据数据库条件和发送的数据,以下else if语句不会发送任何响应,从而导致超时:

    else if (req.body.startDate !== undefined) {
                var startDate = moment(req.body.startDate);

                if (req.body.estimatedEndDate !== undefined) {
                    if (moment(req.body.estimatedEndDate).isBefore(startDate)) {
                        handleError(res, 'Data estimada de conclusão deve ser maior que a data de início');
                    }
                }
                // I MISSED AN ELSE HERE
            }

这是我的错。