在摩卡testing预期的失败

使用摩卡,我试图testing一个构造函数是否抛出一个错误。 我还没有能够使用期望的语法做到这一点,所以我想要做到以下几点:

it('should throw exception when instantiated', function() { try { new ErrorThrowingObject(); // Force the test to fail since error wasn't thrown } catch (error) { // Constructor threw Error, so test succeeded. } } 

这可能吗?

你可以尝试使用Chai的 throw构造。 例如:

 expect(Constructor).to.throw(Error); 

should.js

在should.fail中使用should.js库

 var should = require('should') it('should fail', function(done) { try { new ErrorThrowingObject(); // Force the test to fail since error wasn't thrown should.fail('no error was thrown when it should have been') } catch (error) { // Constructor threw Error, so test succeeded. done(); } }); 

另外你可以使用should throwError

 (function(){ throw new Error('failed to baz'); }).should.throwError(/^fail.*/) 

并用chai使用throw api

 var expect = require('chai').expect it('should fail', function(done) { function throwsWithNoArgs() { var args {} // optional arguments here new ErrorThrowingObject(args) } expect(throwsWithNoArgs).to.throw done() }); 

柴现在有

should.fail()expect.fail()

https://github.com/chaijs/chai/releases/tag/2.1.0

2017答 :使用等待, 不需要任何其他库

 it('Returns a correct error response making a broken order', async function(){ this.timeout(5 * 1000); var badOrder = {} try { var result = await foo.newOrder(badOrder) // The line will only be hit if no error is thrown above! throw new Error(`Expected an error and didn't get one!`) } catch(err) { var expected = `Missing required field` assert.equal(err.message, expected) } }); 

如果你正在使用should.js,你可以(new ErrorThrowingObject).should.throw('Option Error Text or Regular Expression here')

如果你不想要一个单独的图书馆,你也可以做这样的事情:

 it('should do whatever', function(done) { try { ... } catch(error) { done(); } } 

这样,如果testing结束,您就知道错误被捕获了。 否则,你会得到一个超时错误。

Mocha默认使用来自node.js( https://nodejs.org/api/assert.html )的Assert 。 您不需要任何外部库来检查方法是否引发错误。

断言有一个方法 – assert.throws ,它有三个参数,但只有两个真正重要的在这里:

  • 函数 – 这里传递函数, 而不是函数调用
  • 错误 – 在这里通过或对象的构造函数或函数来检查错误

假设你有一个叫做sendMessage(message)的函数,当消息参数没有被设置的时候会引发一个错误。 function代码:

 function sendMessage(message) { if (!message || typeof message !== 'string') { throw new Error('Wrong message'); } // rest of function } 

好吧,为了testing它,你需要额外的function来覆盖input。 为什么? 因为assert.throws不给任何机会传递参数给要testing的函数。

所以,而不是

 // WRONG assert.throws(sendMessage, Error); // THIS IS WRONG! NO POSSIBILITY TO PASS ANYTHING 

你需要创build匿名函数:

 // CORRECT assert.throws(() => { sendMessage(12); // usage of wanted function with test parameters }, Error) 

你能看到区别么? 我不是直接传递函数,而是将函数调用放在匿名函数中,以便用一个准备好的input来调用函数。

第二个参数呢? 这取决于应该抛出什么样的错误,在上面的例子中抛出Error对象,所以我不得不把Error 。 在这个动作的结果中, assert.throws比较抛出的对象是否是同一types的对象。 如果不是Error将会抛出不同的东西,那么这部分需要改变。 例如,而不是Error我会抛出一个Stringtypes的值。

 function sendMessage(message) { if (!message || typeof message !== 'string') { throw 'Wrong message'; // change to String } // rest of function } 

现在testing电话

 assert.throws(() => { sendMessage(12); // usage of wanted function with test parameters }, (err) => err === 'Wrong message') 

而不是在第二个参数中的Error我已经使用比较函数,以比较抛出的错误与期望。

throw (ES2016)

http://chaijs.com/api/bdd/#method_throw

为了清楚起见…这个工作

 it('Should fail if ...', done => { let ret = () => { MyModule.myFunction(myArg); }; expect(ret).to.throw(); done(); }); 

这不起作用

 it('Should fail if ...', done => { let ret = MyModule.myFunction(myArg); expect(ret).to.throw(); done(); });