如何在 chai 中执行 "or"
How to do an "or" in chai should
如何使用 chai.should
进行 or
测试?
例如像
total.should.equal(4).or.equal(5)
或
total.should.equal.any(4,5)
正确的语法是什么?我在文档中找不到任何内容。
查看 Chai expect / should documentation,有几种方法可以进行此测试。
请注意,您可以使用 "and" 链接,但显然不能 "or" - 希望他们有此功能。
- 检查对象是否通过真值测试:
.满足(方法)
@param{ Function }matcher
@param{ String }message_optional_
Asserts that the target passes a given truth test.
示例:
expect(1).to.satisfy(function(num) { return num > 0; });
在您的情况下,要测试 "or" 条件:
yourVariable.should.satisfy(function (num) {
if ((num === 4) || (num === 5)) {
return true;
} else {
return false;
}
});
- 检查一个数字是否在一个范围内:
.within(开始, 结束)
@param{ Number }startlowerbound inclusive
@param{ Number }finishupperbound inclusive
@param{ String }message_optional_
Asserts that the target is within a range.
示例:
expect(7).to.be.within(5,10);
我在给邮递员写测试时遇到了类似的问题。我使用以下脚本解决了问题:
// delete all products, need token with admin role to complete this operation
pm.test("response is ok and should delete all products", function() {
pm.expect(pm.response.code).to.satisfy((status) => status === 204 || status === 404);
});
断言目标是给定数组列表的成员。但是,通常最好断言目标等于其预期值。
expect(1).to.be.oneOf([1, 2, 3]);
expect(1).to.not.be.oneOf([2, 3, 4]);
因为 chai 断言会抛出错误,你可以使用 try/catch 构造:
try {
total.should.equal(4)
} catch (e) {
total.should.equal(5)
}
更困难的案例示例:
try {
expect(result).to.have.nested.property('data.' + options.path, null)
} catch (e) {
expect(result).to.have.property('data', null)
}
在这里,我准确地分享了您需要检查的内容。
expect(true).to.satisfy(() => {
if (total == 4 || total == 5) return true;
else return false;
});
如何使用 chai.should
进行 or
测试?
例如像
total.should.equal(4).or.equal(5)
或
total.should.equal.any(4,5)
正确的语法是什么?我在文档中找不到任何内容。
查看 Chai expect / should documentation,有几种方法可以进行此测试。
请注意,您可以使用 "and" 链接,但显然不能 "or" - 希望他们有此功能。
- 检查对象是否通过真值测试:
.满足(方法)
@param{ Function }matcher
@param{ String }message_optional_
Asserts that the target passes a given truth test.
示例:
expect(1).to.satisfy(function(num) { return num > 0; });
在您的情况下,要测试 "or" 条件:
yourVariable.should.satisfy(function (num) {
if ((num === 4) || (num === 5)) {
return true;
} else {
return false;
}
});
- 检查一个数字是否在一个范围内:
.within(开始, 结束)
@param{ Number }startlowerbound inclusive
@param{ Number }finishupperbound inclusive
@param{ String }message_optional_
Asserts that the target is within a range.
示例:
expect(7).to.be.within(5,10);
我在给邮递员写测试时遇到了类似的问题。我使用以下脚本解决了问题:
// delete all products, need token with admin role to complete this operation
pm.test("response is ok and should delete all products", function() {
pm.expect(pm.response.code).to.satisfy((status) => status === 204 || status === 404);
});
断言目标是给定数组列表的成员。但是,通常最好断言目标等于其预期值。
expect(1).to.be.oneOf([1, 2, 3]);
expect(1).to.not.be.oneOf([2, 3, 4]);
因为 chai 断言会抛出错误,你可以使用 try/catch 构造:
try {
total.should.equal(4)
} catch (e) {
total.should.equal(5)
}
更困难的案例示例:
try {
expect(result).to.have.nested.property('data.' + options.path, null)
} catch (e) {
expect(result).to.have.property('data', null)
}
在这里,我准确地分享了您需要检查的内容。
expect(true).to.satisfy(() => {
if (total == 4 || total == 5) return true;
else return false;
});