【问题标题】:Is supertest compatible with express.js 2.5.8?supertest 是否与 express.js 2.5.8 兼容?
【发布时间】:2013-02-18 19:00:29
【问题描述】:

我正在使用 express.js 版本 2.5.8(来自旧代码),并希望使用 supertest 测试路由加载。我在服务器运行时遇到问题,但没有停止。我使用jasmine-node 运行测试,这表明我的断言成功。但是,控制台显示该进程仍在运行。

var request = require('supertest')
  , express = require('express');

describe('Example Test', function() {
  var app;

  beforeEach(function() {
    app = express.createServer();

    app.configure(function() {
      app.set('port', process.env.PORT || 3000);
      app.use(express.logger('dev'));
      app.use(express.bodyParser());
      app.use(express.methodOverride());
    });

    app.get('/', function(req, res){
      res.send(200);
    });
  });

  it('index should return 200', function(done) {
    request(app)
    .get('/')
    .expect(200)
    .end(function(err, res) {
      expect(err).toBe(null);
      done();
    });
  });
});

此示例改编自可能使用 express.js 3.x.x 的示例。我的假设是快速服务器的行为不同,导致当请求在测试内终止时它不会关闭。我不确定如何纠正这个问题。

【问题讨论】:

    标签: testing express jasmine jasmine-node supertest


    【解决方案1】:

    服务器仍在运行,因为您没有在任何地方关闭它。只需将app.close() 添加到您的end(function(){}) 即可停止快速服务器。如果您还想退出节点,请使用process.exit()

    【讨论】:

    • 谢谢你,@zeMirco。我可以发誓我已经尝试过这个解决方案。事实上,我确实曾经有过app.close()。不过,它一定不在正确的位置。
    猜你喜欢
    • 2013-09-18
    • 1970-01-01
    • 2017-06-07
    • 2011-05-23
    • 2011-10-22
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多