根据http://chaijs.com/api/bdd/#a , a
/an
可用于检查变量的类型。
.a(type)
@param{ String } type
@param{ String } message _optional_
The
a
andan
assertions are aliases that can be used either as language chains or to assert a value's type.
但是,我无法检查整数的变量。给定的例子,例如expect('1337').to.be.a('string');
为我工作,但以下不适合:
expect(42).to.be.an('integer');
expect(42).to.be.an('Integer');
expect(42).to.be.an('int');
expect(42).to.be.an('Int');
在运行 mocha 时,它们都给我以下错误:
Uncaught AssertionError: expected 42 to be an integer
如何用 chai 测试一个整数的变量?
最佳答案
有点晚了,但是对于来自搜索引擎的人来说,这是另一个解决方案:
var expect = require('chai').expect
expect(foo).to.be.a('number')
expect(foo % 1).to.equal(0)
由于 true % 1 === 0
或 null % 1 === 0
等原因,需要进行数字检查。
关于javascript - Chai 单元测试-expect(42).to.be.an ('integer'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23597475/