【问题标题】:Assert an error is thrown using the new to execute a constructor function [duplicate]断言使用 new 执行构造函数会引发错误[重复]
【发布时间】:2013-12-30 12:14:13
【问题描述】:

将 Chai 与 Mocha 一起使用,当使用 new 关键字执行构造函数时,我将使用什么语法来断言抛出错误?使用以下内容时出现错误:

assert.throw(new SomeFunction, Error);

这会返回:

AssertionError: expected { Object () } to be a function

【问题讨论】:

    标签: javascript mocha.js chai


    【解决方案1】:

    将函数传递给assert.throw

    assert.throw(function () {
        new SomeFunction()
    }, Error);
    

    您没有工作的原因是new SomeFunction 被解释为new SomeFunction() 并在assert.throw 执行之前执行。因此,您最终会使用作为 SomeFunction 实例的对象来运行 assert.throw,而不是使用实例化对象的函数。

    【讨论】:

      猜你喜欢
      • 2017-08-18
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 2014-12-17
      相关资源
      最近更新 更多