Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

How to do an "or" in chai should

How do I do an or test with chai.should?

e.g. something like

total.should.equal(4).or.equal(5)

or

total.should.equal.any(4,5)

What's the right syntax? I couldn't find anything in the documentation.

question from:https://stackoverflow.com/questions/32387293/how-to-do-an-or-in-chai-should

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Viewing the Chai expect / should documentation, there are several ways to do this test.

Note that you can chain using "and" but apparently not "or" - wish they had this functionality.

  1. Check whether an object passes a truth test:

.satisfy(method)

@param{ Function }matcher
@param{ String }message_optional_

Asserts that the target passes a given truth test.

Example:

expect(1).to.satisfy(function(num) { return num > 0; });

In your case, to test an "or" condition:

yourVariable.should.satisfy(function (num) {
    if ((num === 4) || (num === 5)) {
        return true;
    } else {
        return false;
    }
});
  1. Check whether a number is within a range:

.within(start, finish)

@param{ Number }startlowerbound inclusive
@param{ Number }finishupperbound inclusive
@param{ String }message_optional_

Asserts that the target is within a range.

Example:

expect(7).to.be.within(5,10);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...