我正在使用should.js框架(v8.2.x)进行单元测试,并且一直在使用一些非常基本的测试。然而,由于考试失败,我遇到了这个问题,这使我很困惑。
我定义了这个虚拟函数来测试,add
var add = function(a, b) {
if (isNaN(a) || isNaN(b)) {
throw new Error('One of the arguments is not a number');
}
return +a + +b
};
下面是我的模拟测试:
should.equal(add('1', '1'), '2'); // passes
add('1', '1').should.equal('2') // fails!
现在,根据他们的github,should(something)
和something.should
通常返回相同的东西,但是没有关于差异的其他信息。
根据他们的API文档,should.equal
和assert.equal
是一样的。但这次测试我通过了:
assert.equal(add('1','1'), '2'); // passes
,所以我有三个问题:
add('1', '1').should.equal('2')
不及格?should.equals
和assert.equals
有不同的行为时,他们会说它们是相同的?发布于 2016-03-07 08:45:49
回答你的问题和你做错了什么。
现在根据他们的github,应该(某物)和something.should通常返回相同的东西,但是没有关于区别的额外信息。
should(something)
和something.should
通常是一样的。但是您使用了should.equal,docs没有提到它与其他方法的等价性。它就是复本 node.js assert.equal
为什么添加(‘1’,'1').should.equal('2')不通过?
因为docs明确表示它在内部使用===。
为什么应该产生不同结果的两种用法?
因为这是不同的方法,没有人说它应该是平等的。
为什么当should.equals和assert.equals有不同的行为时,他们会说它们是相同的?
$ node
> var should = require('.')
undefined
> var assert = require('assert')
undefined
> var add = function(a, b) {
... if (isNaN(a) || isNaN(b)) {
..... throw new Error('One of the arguments is not a number');
..... }
... return +a + +b
... };
undefined
> should.equal(add('1', '1'), '2');
undefined
> assert.equal(add('1', '1'), '2');
undefined
因此,为了澄清您的错误:在should.js中存在与assert.equal
相同的should.equal(a, b)
。也存在something.should.equal(other)
,它在内部使用===
(因为存在.eql
可以进行深度相等的检查),should(somethng).equal(other)
也与something.should.equal(other)
相同(但不适用于标准包装器)。
希望它能表明这一点。
https://stackoverflow.com/questions/35833999
复制相似问题