【发布时间】:2014-10-24 06:55:50
【问题描述】:
这是一个 NodeJS 模块:
var plural = function(words, num) {
if (words.length != 3) {
throw new Error('Wrong words array');
}
var lastNum = num % 10;
var second = (num / 10) % 10;
if (second == 1)
return words[2]
else if (lastNum == 1)
return words[0];
else if (lastNum <= 4)
return words[1];
else
return words[2];
};
module.exports = plural;
这是模块测试:
var expect = require('chai').expect;
var plural = require('../plural')
describe('Test with wrong array', function() {
it('Must throw Error', function() {
expect(plural(['я','меня'])).to.throw(Error);
});
});
我想测试异常抛出。但这是mocha 的输出:
Test with wrong array
1) Must throw Error
1 failing
1) Test with wrong array Must throw Error:
Error: Wrong words array
at plural (C:\Users\home\Google Drive\Учеба\Спецкурсы\Яндекс.Интерфейсы\TestableCode\testable-code-01\plural.js:3:9)
at Context.<anonymous> (C:\Users\home\Google Drive\Учеба\Спецкурсы\Яндекс.Интерфейсы\TestableCode\testable-code-01\test\cat.test.js:31:10)
at callFn (C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:250:21)
at Test.Runnable.run (C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:243:7)
at Runner.runTest (C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:373:10)
at C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:451:12
at next (C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:298:14)
at C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:308:7
at next (C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:246:23)
at Object._onImmediate (C:\Users\home\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:275:5)
at processImmediate [as _immediateCallback] (timers.js:345:15)
所以,我的代码抛出异常,这是正确的行为,应该通过测试。但不是。有什么问题?
【问题讨论】:
-
您必须通过引用将函数传递给 expect() - 请参阅下面的更新答案和test cases
标签: javascript node.js mocha.js chai