在使用Mocha测试api时,出现Timeout of 2000ms exceeded的错误
发布于 9 年前 作者 soaph 14519 次浏览 最后一次编辑是 8 年前 来自 问答

这是我的mocha测试文件 test-api.js :

var should = require('should');
var mongoose = require('mongoose');
var request = require('supertest');
var db = require('../config/db');
var post = require('../app/models/post');
var faker = require('faker');


describe('Post', function () {
    var url = 'http://localhost:3000';

    before(function (done) {
        var db_url = db.url_test;
        mongoose.connect(db_url, done);
    });

    it('should post a post and get the post back', function (done) {
        var _body = {
            title: faker.name.title(),
            body: faker.lorem.paragraph()
        };
        //console.log(_body);
        request(url)
            .post('/api/post')
            .send(_body)
            .end(function (err, res) {
                //if (err) {
                //    console.log(err);
                //    //console.log('/api/post post method failed!');
                //}
                //console.log('res.body: ');
                //console.log(res.body);
                res.body.should.have.property('url');
                res.body.should.have.property('title');
                res.body.should.have.property('body');
                res.body.should.have.property('date');
                done();
            });
    });

    it('should get all the posts', function (done) {
        request(url)
            .get('/api/post')
            .expect(200)
            .end(function (err, res) {
                if (err) {
                    console.log(err);
                    //console.log('/api/post get method failed!')
                }
                res.body.should.have.property('count');
                res.body.should.have.property('limit');
                res.body.should.have.property('page');
                res.body.should.have.property('results');
                //console.log(res.body);
                done();
            });
    });
});

我觉得问题出在before函数里面,因为如果我把mongoose.connect()放在server.js里面 (这是我的api服务器的启动文件), 我就可以正常通过测试.

但是我想在测试文件中临时指定单独的测试用数据库,而不是在server.js里面就指定好,所以把mongoose.connect()放在测试文件内.

这是上面这个mocha测试文件的测试结果:

  Post
    1) should post a post and get the post back
    ✓ should get all the posts (52ms)


  1 passing (2s)
  1 failing

  1) Post should post a post and get the post back:
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

我现在也不知道要怎么处理这个问题了,求各位大神帮忙啊!(T_T)…先谢谢大家了!

8 回复
 describe('#get_all_unchecked()', function() {
    it('should return get_all_unchecked().length() > 0 when only 1 product', function(done) {
      this.timeout(15000);
    
      Product.get_all_uncheckedAsync().then(function(products){
        var product = products[0]
        //console.log("spread" + product.name);
        assert.equal(product.name, 'get all');
        done()
      }).catch(function(err) {
        console.log("There was an error" + err);
        done(err)
      });
      
    });
	```

this.timeout(15000);

@i5ting 在这个测试文件中每一个it里面都要用这个吗?

@i5ting 我想问一下为什么要这样来结局?是哪里的问题啊?

你根本不会用supertest。。。。

@i5ting 。。。。不行

感觉是你的’/api/post’的post router 处理上在依赖其它的非测试环境,你在这个post处理逻辑里面加些log,看看执行情况呗, 既然get测试过了,应该不是db连接的问题

找到问题的解决办法了,2楼说的对,我supertest用错了。

我应该在测试文件里面导入api所在的app,然后放到request()参数表里面,而不是用url,这样就可以通过。

如果不在测试文件里面直接对app发送request,而是用url来进行发送的话,api里面会收到request,但是不会产生任何response。

可能原因是这个时候database的连接在api里不可用吧。

回到顶部