【发布时间】:2016-09-22 19:38:08
【问题描述】:
下面的代码 sn -p 非常简单(来自https://mochajs.org/#synchronous-code)。感觉很愚蠢,但是,为什么 [1,2,3] 与文字符号一起使用时会计算为 undefined,而不是在 myArray 变量中使用时?
var assert = require('assert') // "mocha": "^3.0.2"
var should = require('should') // "should": "^11.1.0"
describe('Array', function () {
describe('#indexOf()', function () {
var myArray = [1, 2, 3]
it('Should return -1 when the value is not present', function () {
myArray.indexOf(0).should.equal(-1) // a - success
[1, 2, 3].indexOf(0).should.equal(-1) // b - fails test
})
})
})
当我运行测试时,'b' 行失败如下:
Array
#indexOf()
1) Should return -1 when the value is not present
1) Array #indexOf() Should return -1 when the value is not present:
TypeError: Cannot read property 'indexOf' of undefined
... Error trace just points the line where it fails, nothing else ...
我希望能对这个令人不安但很容易回答的问题有所了解。干杯。
【问题讨论】:
标签: javascript testing mocha.js should.js