[英]Successfully changed the Immutable or Primitive Data-Types in JS. Then How these are Primitives or is JS Concepts wrong?
[英]How to test the data-types of number and null in Chai and Postman?
我正在尝试使用 Postman 在我的 API 测试中验证响应的数据类型。 返回值可以是整数、浮点数或 null 。 我怎样才能用 Chai 和在一个声明中做到这一点?
这些是我在使用 javascript 和 Chai 的 Postman 测试中的简化断言。
pm.expect(23).to.be.a('number'); // PASS
pm.expect(2.3).to.be.a('number'); // PASS
pm.expect(23).to.be.oneOf(['number', null]); // FAIL
pm.expect(2.3).to.be.oneOf(['number', null]); // FAIL
我希望所有的断言都能通过。 谢谢,我感谢任何评论。
所以我认为您拥有一种专门的方法,但是我不认为这种方法存在,所以我有所作为。 我没有使用一个预定义的状态,而是自己制作了stament并将其评估为true
这里的主要问题是typeof(null)返回“对象”,我认为这不足以作为比较
let element=[23,null,12];
for(let e of element){
//one statement
pm.expect(e==null||typeof(e)=='number').to.be.true
}
由于JavaScript中的object
类型为null
,因此将测试用例写为
pm.expect(typeof 23).to.be.oneOf(['number', 'object']);
这可能是一种简单的方法,并且可以在简单的检查中使用。 但是,我建议您使用第二种方法,即使用最快的JSON模式验证器。
var Ajv = require('ajv'),
ajv = new Ajv({logger: console}),
schema = {
"properties": {
"myNumber": {
"type": ["number","null"]
}
}
};
pm.expect(ajv.validate(schema, {myNumber: 23})).to.be.true;
您可以使用来自 chai 的.statify
方法:
expect(1).to.satisfy(function(num) {
return num == null || Number.isInteger(num);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.