【发布时间】:2014-09-22 02:30:31
【问题描述】:
好的,我正在搜索几个小时的解决方案,以便在 Jenkins 中为我的 Express JS 应用运行我的 Mocha 单元测试。
编写测试很容易,但是将测试与我的应用程序连接起来有点困难。我当前测试的一个例子:
/tests/backend/route.test.js
var should = require('should'),
assert = require('assert'),
request = require('supertest'),
express = require('express');
describe('Routing', function() {
var app = 'http://someurl.com';
describe('Account', function() {
it('should return error trying to save duplicate username', function(done) {
var profile = {
username: 'vgheri',
password: 'test',
firstName: 'Valerio',
lastName: 'Gheri'
};
// once we have specified the info we want to send to the server via POST verb,
// we need to actually perform the action on the resource, in this case we want to
// POST on /api/profiles and we want to send some info
// We do this using the request object, requiring supertest!
request(app)
.post('/api/profiles')
.send(profile)
// end handles the response
.end(function(err, res) {
if (err) {
throw err;
}
// this is should.js syntax, very clear
res.should.have.status(400);
done();
});
});
});
在上面的示例中,我连接到一个正在运行的应用程序(参见 ```var app = 'http://someurl.com'``)。显然,这在 Jenkins 内部不起作用,除非我可以告诉 Jenkins 首先运行应用程序,然后检查 localhost url。但是如何做到这一点呢?
现在,如果我看一下 https://www.npmjs.org/package/supertest,这应该足以测试我的 express 应用了:
var request = require('supertest')
, express = require('express');
var app = express();
但事实并非如此。我在所有要测试的 url 上收到 404 错误。
有谁知道如何在 Jenkins 中测试我的应用程序?
【问题讨论】:
标签: javascript node.js express jenkins