gpt4 book ai didi

testing - supertest 与 express.js 2.5.8 兼容吗?

转载 作者:行者123 更新时间:2023-11-28 20:09:06 25 4
gpt4 key购买 nike

我正在使用 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 的示例。我的假设是 express 服务器的行为不同,导致当请求在测试中终止时它不会关闭。我不确定如何解决这个问题。

最佳答案

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

关于testing - supertest 与 express.js 2.5.8 兼容吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14943478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com