【问题标题】:ChaiJS doesn't handle exceptionsChaiJS 不处理异常
【发布时间】: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


【解决方案1】:

Chai 的 to.throw() 方法是 assertThrows 方法的简写,它需要传递几个参数。你可以看到一个 to.throw() here 的例子。看起来需要将 throws() 传递给您将抛出的错误的构造函数。

如果您有兴趣抛出自定义错误消息,可以在 Chai 的单独库 'assertion-error' here 中查看 AssertionError 的定义。

在你的情况下,你应该可以传入Error.constructorError 我想达到同样的效果。

expect(plural(['я','меня'])).to.throw(Error);

此外,您必须通过引用传递期望的函数 - 在函数被用作期望的参数之前,您正在 执行该函数:

expect(function(){
   plural(['я','меня']);
}).to.throw(Error);

【讨论】:

  • 测试失败,expect(plural(['я','меня'])).to.throw(Error.constructor);
  • 本质上,你需要做的最后一步是找出你的 Error 函数的构造函数是什么。当你调用“throw new Error()”时,它应该只使用 Error 作为它的构造函数——你不是在任何地方重新定义 Error 全局吗?
【解决方案2】:

也许这行:

expect(plural(['я','меня'])).to.throw();

应该阅读

expect(plural(['я','меня'])).to.throw(Error);

【讨论】:

    【解决方案3】:

    如果我只是将您的 expect(plural... 行替换为

    expect(plural.bind(undefined, ['я','меня'])).to.throw(Error);
    

    然后就可以了。

    正如 netpoetica 所解释的,你不能只将 plural(...) 放在 expect 中,因为 plural 被调用 before expect 执行,而 expect 得到的是 plural 的返回值,但由于它引发异常,expect 无法执行。此外,expect 想要的是一个 it 将调用以检查它是否引发异常的函数。 netpoetica 使用匿名函数来做到这一点。我更喜欢使用bindbind 在上面的代码中所做的是从 plural 创建一个新函数,调用该函数时会将 this 的值设置为 undefined,并将第一个参数设置为 ['я','меня']

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多