摩卡:单个服务器请求中的多个“it”调用
Mocha: Multiple "it" calls in single server request
我正在尝试使用 Mocha 测试 40+ API 端点。我想执行一些子测试作为单个服务器调用的一部分。
例如,我想测试是否it('returns valid JSON...
和it('returns a valid status code...
等。
configs.forEach(function(config) {
describe(config.endpoint, () => {
it('...', function(done) {
server
.post(config.endpoint)
.send({})
.expect('Content-type', /json/)
.expect(200)
.end(function(err, res) {
//it('has a proper status code', () => {
expect(res.status).toEqual(200);
//})
//it('does not have an error object', () => {
expect(res.body.hasOwnProperty('error')).toEqual(false);
//})
done();
})
})
})
})
问题是我无法嵌套it
语句,但我依靠回调,通过done()
来指示何时收到响应,所以我必须将调用包装在一个it
语句中......
因为其中一些请求需要半秒才能解决,而且有 40+ 个,所以我不想为这些请求创建单独的测试。创建单独的测试也会复制 config.endpoint,我想看看每个端点的测试是否都在一个地方传递。
如何为单个服务器调用创建多个测试?
以下是我如何使用摩卡、柴和超级测试(API 请求)完成此操作:
import { expect } from "chai"
const supertest = require("supertest");
const BASE_URL = process.env.API_BASE_URL || "https://my.api.com/";
let api = supertest(BASE_URL);
describe("Here is a set of tests that wait for an API response before running.", function() {
//Define error & response in the 'describe' scope.
let error, response;
//Async stuff happens in the before statement.
before(function(done) {
api.get("/dishes").end(function(err, resp) {
error = err, response = resp;
done();
});
});
it("should return a success message", function() {
console.log("I have access to the response & error objects here!", response, error);
expect(response.statusCode).to.equal(200);
});
it("should return an array of foos", function() {
expect(response.body.data.foo).to.be.an("array");
});
});
configs.forEach(function(config) {
describe(config.endpoint, () => {
var response;
it('...', function(done) {
server
.post(config.endpoint)
.send({})
.expect('Content-type', /json/)
.expect(200)
.end(function(err, res) {
response=res;
done();
})
});
it('has a proper status code', () => {
expect(response.status).toEqual(200);
})
it('does not have an error object', () => {
expect(response.body.hasOwnProperty('error')).toEqual(false);
})
})
})
这个呢?我不确定测试用例的嵌套,但它对您有用。
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 如何从Java/scala调用js美化程序
- 如何调用这个匿名 JavaScript 函数
- 如何从模块链中调用函数.导出到节点中
- 我需要从php调用javascript或jquery
- Chrome开发工具(如何知道我在调用哪个javascript对象)
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- Twitter Bootstrap typeahead:使用“this”获取上下文/调用元素
- node.js:setInterval()正在跳过调用
- 如何在单击复选框后调用控制器方法
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- 使用Google Visualization动态调用构造函数
- FileReader JS Api在它之前调用Fire'It’准备好了
- 调用JavaScript-它'It’不起作用.
- 每次调用变量it'时,我如何防止Math.random()返回不同的值;s存储在下
- 什么'It’绑定和调用的区别是什么
- 什么'It’调用函数时把()放在末尾是不对的
- 摩卡:单个服务器请求中的多个“it”调用