节点.js + Chai/Mocha/Should:针对同一响应运行多个测试
Node.js + Chai/Mocha/Should: running multiple tests against the same response
我正在使用与以下内容非常相似的东西来使用摩卡执行一系列 API 测试。这很好,但它需要为每个测试进行单独的 API 调用。我想使用相同的 API 调用并针对该响应运行多个测试。我一直在读到您可以使用before
来做到这一点,但是网络上没有一个示例实际上显示它与 API 调用一起工作?
var chai = require('chai');
var request = require('request');
var async = require('async');
var assert = chai.assert,
expect = chai.expect,
should = chai.should();
describe('/', function () {
it('should return 200', function (done) {
request.get('http://localhost:8000', function (err, res, body) {
res.should.have.status(200);
done();
});
});
it('should say "Hello, world!"', function (done) {
request.get('http://localhost:8000', function (err, res, body) {
body.should.have.property('type', 'aType');
done();
});
});
});
你可以
用这样的before
函数来做到这一点......
var chai = require('chai');
var request = require('request');
var async = require('async');
var assert = chai.assert,
expect = chai.expect,
should = chai.should();
describe('/', function () {
var firstRequest;
before(function(done) {
request.get('http://localhost:8000', function(err, res, body) {
firstRequest = {
err:err,
res:res,
body:body
};
done();
});
});
it('should return 200', function (done) {
firstRequest.res.should.have.status(200);
done();
});
it('should say "Hello, world!"', function (done) {
firstRequest.body.should.have.property('type','aType');
done();
});
});
但是,除非您有充分的理由这样做,否则我认为您最好只是组合测试。
var chai = require('chai');
var request = require('request');
var async = require('async');
var assert = chai.assert,
expect = chai.expect,
should = chai.should();
describe('/', function () {
it('should return 200 and say "Hello, world!"', function (done) {
request.get('http://localhost:8000', function (err, res, body) {
res.should.have.status(200);
body.should.have.property('type', 'aType');
done();
});
});
});
如果测试失败,即使有两个断言,摩卡也会报告测试失败的具体原因。
相关文章:
- 循环遍历 Angular HTTP 请求中的对象,等到响应后再运行下一个循环
- 运行 ajax 函数以每 3 秒检查一次 json 响应
- jQuery ajax:error 即使响应为 OK 200 也会运行
- 如果服务器响应特定内容,则运行 (JS) 函数
- 如果我的响应在运行时更改结构,如何生成动态页面
- AngularJS在$http响应后运行编译指令
- 节点.js + Chai/Mocha/Should:针对同一响应运行多个测试
- 为什么在未读取响应的情况下,节点http客户端仍在运行
- 响应式导航在移动设备上未按预期运行
- JavaScript和UI响应中的长时间运行循环
- 未能在运行时获取响应
- 运行请求,直到响应是用$q请求的
- 如何仅在响应性文档的特定字段发生更改时运行跟踪器计算
- Jquery上的Promise回调在空响应时永远不会运行
- 流星响应式搜索代码未运行
- Chrome扩展运行时.Sendmessage等待响应
- 当多个API调用时,只运行一次响应拦截器
- 在Android应用程序中运行HTML5网站-没有响应消息
- 为什么当响应变量改变值时,这个函数不运行?
- AJAX响应200,但没有来自XAMPP本地主机上运行的PHP脚本的JSON响应数据