Zor*_*ene 9 node.js jasmine supertest
我正在使用通过NPM安装的Jasmine 2.3并使用Grunt执行.
'use strict';
module.exports = function(grunt) {
grunt.initConfig({
package: grunt.file.readJSON('package.json'),
exec: {
jasmine: 'node_modules/.bin/jasmine'
}
});
require('load-grunt-tasks')(grunt);
require('time-grunt')(grunt);
grunt.registerTask('default', 'exec:jasmine');
};
Run Code Online (Sandbox Code Playgroud)
我导出了一个Express.js应用程序对象,并在我的规范中与SuperTest一起使用它.
'use strict';
var supertest = require('supertest')
var application = require('../../../../../server');
describe('GET /api/users', function() {
it('should respond with json', function(done) {
supertest(application)
.get('/api/users')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, done);
});
});
Run Code Online (Sandbox Code Playgroud)
当我运行规范时,即使200状态代码是预期的并且404是结果,我也没有错误.是Jasmine或SuperTest中的问题,还是我应该使用SuperAgent.
我没有路由设置只是404Express应用程序对象上的错误处理程序设置.
application.use(function(request, response, next) {
response
.status(404)
.send({
message: 'not found'
});
});
Run Code Online (Sandbox Code Playgroud)
Bre*_*nan 10
首先,好问题!我已经学到了一些研究这个的东西.显然Jasmine + Supertest并不能很好地融合在一起.其原因似乎是Supertest呼唤done(err),但Jasmine只会在被叫done.fail()或fail()被叫时失败.你可以看到一些github上的问题在这里,并在这里.
使用此代码查看证明:
describe('GET /api/users', function() {
it('should respond with json', function(done) {
supertest(application)
.get('/api/users')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, function(res){
console.log(res);
//res is actually an error because supertest called done(error)!
done.fail() //Now jasmine will actually fail
});
});
});
Run Code Online (Sandbox Code Playgroud)
鉴于此,似乎最简单的解决方案是使用可以很好地协同工作的备用库.我个人使用摩卡代替茉莉,取得了很好的成功.这是你的选择!
如果你真的想,你可以使用jasmine并编写自己的验证器,在这里的supertest文档中看到.
我希望这有帮助!
| 归档时间: |
|
| 查看次数: |
3366 次 |
| 最近记录: |