如何使用Jasmine 2.3和SuperTesttestingExpress.js路由
我使用通过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'); };
我导出了一个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); }); });
当我运行规范时,即使预计有200
状态码, 404
也是结果,但是我没有遇到任何错误。 在Jasmine或SuperTest的问题,或者我应该使用SuperAgent 。
我没有路由设置快速应用程序对象上的404
error handling程序设置。
application.use(function(request, response, next) { response .status(404) .send({ message: 'not found' }); });
首先,好问题! 我学到了一些研究这个的东西。 显然茉莉花+ Supertest在一起玩的不是很好。 这样做的原因似乎是Supertest调用done(err)
,但是Jasmine只会在done.fail()
或fail()
被调用时fail()
。 你可以在这里看到一些github的问题。
使用此代码来查看certificate:
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 }); }); });
鉴于此,似乎最简单的解决scheme是使用可以很好地一起玩的备用库。 我亲自用摩卡代替茉莉花,取得了不错的成绩。 这是你的select!
如果你真的想,你可以使用茉莉花,并在超文本文件中看到你自己的validation器。
我希望这有帮助!
testing失败的原因是因为Supertest调用done(err)
而茉莉花失败done.fail()
或fail()
。 尽pipe如此,还是有办法让Jasmine和Supertest合作。
看一下这个:
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) .end(function(err, res) { if (err) { done.fail(err); } else { done(); } }); }); });
另外
.end(function(err, res) { if (err) { done.fail(err); } else { done(); } });
在所有Supertests end
的描述块应该解决Supertest和Jasmine之间的问题。 这也将在控制台中提供一些甜美的错误描述。
为了避免在每个.end()
函数中写入逻辑。 我build议在所有的jasmine规范之前使用下面的代码片段(或者如果你愿意,可以把它放在helper helpers/tell-jasmine.js
):
function tellJasmine(done) { return function (err) { if (err) { done.fail(err); } else { done(); } }; }
现在你可以在超类的末尾写上.end(tellJasmine(done))
:
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) .end(tellJasmine(done)); }); });
这个代码信贷去nesQuick 。
- 检测不需要的function(fdescribe,describe.only)作为Gulp任务
- 如何从grunt执行节点模块的茉莉花testing
- Supertest期望不正确地声明状态码
- Webstorm中运行茉莉花testing停止跟踪与退出码0
- 使用Jasmine-Node进行testing时,Mongoose模型自定义函数中消失的值
- node.jsunit testing模拟依赖
- PhantomJS退出意外退出代码-1073741819
- 为什么我的茉莉花testing在DEFAULT_TIMEOUT_INTERVAL之前超时?
- 如何debugging从Visual Studio代码中的Grunt运行茉莉花testing?