javascript Chai 单元测试 - expect(42).to.be.an('integer')
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23597475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chai unittesting - expect(42).to.be.an('integer')
提问by soerface
According to http://chaijs.com/api/bdd/#a, a
/an
can be used to check for the type of a variable.
根据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.
。一种)
@param{ String } type
@param{ String } message _optional_
的
a
和an
断言是可以用来作为语言链或断言值的类型的别名。
However, I'm not able to check for the variable beeing an integer. The given examples, e.g. expect('1337').to.be.a('string');
work for me, but the following does not:
但是,我无法检查变量是否为整数。给定的例子,例如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');
All of them are giving me the following error when running mocha:
运行 mocha 时,所有这些都给我以下错误:
Uncaught AssertionError: expected 42 to be an integer
How do I test with chai for a variable beeing an integer?
如何使用 chai 测试变量 beeing 整数?
采纳答案by Jeremy J Starcher
JavaScript doesn't have a separate integer
type.
JavaScript 没有单独的integer
类型。
Everything is a IEE 754 floating point number, which would of type number
.
一切都是IEE 754 浮点数,类型为number
.
回答by tleb
A bit late, but for people coming from search engines, here is another solution:
有点晚了,但对于来自搜索引擎的人来说,这是另一种解决方案:
var expect = require('chai').expect
expect(foo).to.be.a('number')
expect(foo % 1).to.equal(0)
The number check is required because of things such as true % 1 === 0
or null % 1 === 0
.
由于true % 1 === 0
或等原因,需要进行数字检查null % 1 === 0
。
回答by Convolver
This is also possible (at least whithin node):
这也是可能的(至少在节点中):
expect(42).to.satisfy(Number.isInteger);
Here is a more advanced example:
这是一个更高级的例子:
expect({NUM: 1}).to.have.property('NUM').which.is.a('number').above(0).and.satisfy(Number.isInteger);
回答by Bran van der Meer
I feel your pain, this is what I came up with:
我感受到你的痛苦,这就是我想出的:
var assert = require('chai').assert;
describe('Something', function() {
it('should be an integer', function() {
var result = iShouldReturnInt();
assert.isNumber(result);
var isInt = result % 1 === 0;
assert(isInt, 'not an integer:' + result);
});
});
回答by Bob_Gneu
Depending on the browser/context you are running in there is also a function hanging off of Number that would be of some use.
根据您运行的浏览器/上下文,还有一个挂在 Number 上的函数会有用。
var value = 42;
Number.isInteger(value).should.be.true;
It has not been adopted everywhere, but most of the places that matter (Chrome, FFox, Opera, Node)
它尚未在所有地方采用,但在大多数重要的地方(Chrome、FFox、Opera、Node)
回答by rodrigo-silveira
Another [not optimal] solution (why not?!)
另一个 [非最佳] 解决方案(为什么不呢?!)
const actual = String(val).match(/^\d+$/);
expect(actual).to.be.an('array');
expect(actual).to.have.lengthOf(1);