【问题标题】:Mocha tests fail when being run from gulp从 gulp 运行摩卡测试失败
【发布时间】:2016-05-27 23:11:02
【问题描述】:

我为我的应用程序的服务级别类开发了一套测试,并且在 mocha 中运行时所有测试都通过了:

mocha testfile.js

但是,当我从 gulp 运行它们时,我在其中一个文件中的每个测试都会遇到超时错误:

1) 预订服务应创建预订: 错误:超过 2000 毫秒的超时。确保在此测试中调用了 done() 回调。

2) 预订服务应取消预订: 错误:超过 2000 毫秒的超时。确保在此测试中调用了 done() 回调。

...每次测试都以此类推

我的 gulpfile 看起来像这样:

var serviceTests = [
  './test/block-service-test.js',
  './test/booking-service-test.js',
  './test/location-service-test.js',
  './test/logging-service-test.js'
];

gulp.task('serviceTests', function(){
  gulp.src(serviceTests)
    .pipe(mocha()) 
    .on('end', function() { console.log('>>Finished Running Tests'); })
    .pipe(exit());
});

我唯一能想到的是失败的测试是通过猫鼬连接到数据库的。以下是设置示例和其中一项测试:

process.env.MONGOLAB_URI = 'mongodb://localhost:27017/TestDB';
process.env.IN_TEST = true;

var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
mongoose.connect(process.env.MONGOLAB_URI);

describe("booking service", function() {

    var start = moment(new Date());
    var end = moment(start).add(2, 'h');
    var tutorId = "SomeTutorId";
    var userId = "SomeUserId";

    it("should create booking", function(done){
        return bookingService.createBooking(tutorId, userId,  start, end, "123 Anywhere St", "Test", "User", "Tutor", "LastName")
        .then(function(booking){
            assert.notEqual(booking, null);
            done();
        }).catch(function(err){
            console.log(err);
            assert.fail();
            done();
        });
    });
});

任何想法为什么他们可以使用 mocha 而不是 gulp?

【问题讨论】:

  • 不知道为什么这个问题被否决了?这是一个合法的问题,猫鼬应该给出与关闭连接相关的错误。

标签: node.js gulp mocha.js


【解决方案1】:

编写了一个关闭数据库连接的测试。 mongoose 不会给出“关闭连接”错误,而是超时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2019-09-07
    • 1970-01-01
    相关资源
    最近更新 更多