如何编写一个检查chai中多种类型的测试

时间:2014-12-17 21:35:26

标签: unit-testing chai

如果输入是字符串或空值

,我正在尝试编写一个测试 在柴的

有类似的东西

expect(foo).to.be.a('string').or.a('null')

如果不是在编写需要检查多种类型的测试时最佳做法是什么?

4 个答案:

答案 0 :(得分:6)

这可能是最简单的方法,因为没有or关键字。

var str = null;

expect(str).to.satisfy(function(s){
    return s === null || typeof s == 'string'
});

答案 1 :(得分:0)

你传递给chai' assert的第一个参数是一个表达式,所以你可以这样做:

assert(assert.isString(foo) || assert.isNull(foo), 'must be a string or null');

答案 2 :(得分:0)

Chai提供了一种oneOf方法,该方法采用了一系列可能的匹配项。 因此,OP的类型可以是字符串或null的断言可以这样编码……

expect(foo).to.be.oneOf(['string', null])

答案 3 :(得分:0)

解决方案:

var str = null;
expect(str).to.satisfies(output=>!output); // testcase will pass

var str = '';
expect(str).to.satisfies(output=>!output); // testcase will pass

var str = 'test';
expect(str).to.satisfies(output=>!output); // testcase will fail