按照承諾測試Chai的拒絕

[英]Test a rejection with Chai as promised


I want to test a function returning a promise.

我想測試一個返回promise的函數。

In this particular test, the promise is expected to be rejected with an Error object containing the classical message field (in this test, it is expected to equal "my error message") and a custom field I added named code, which is a string (like "EACCESS", "ERIGHT", etc, in this test it is expected to equal "EFOO")

在這個特定的測試中,承諾將被拒絕,其中一個Error對象包含經典消息字段(在此測試中,它應該等於“我的錯誤消息”)和一個自定義字段我添加了命名代碼,這是一個字符串(如“EACCESS”,“ERIGHT”等,在此測試中它應該等於“EFOO”)

I want to use chai-as-promised for that.

我想使用chai-as-promised。

return expect(foo()).to.eventually.be.rejectedWith("my error message");

This assertion is working but now I would like to test the code field too.
How to do that?

這個斷言正在起作用,但現在我也想測試代碼字段。怎么做?

4 个解决方案

#1


34  

If you're using Chai-As-Promised (as you say you are), then it allows for chaining off of rejectedWith - and it sets the chain assertion object to be the error object - meaning anything after rejectedWith() is now going to assert on the Error. This lets you do cool things like:

如果你正在使用Chai-As-Promised(就像你說的那樣),那么它允許鏈接被拒絕的 - 並且它將鏈斷言對象設置為錯誤對象 - 這意味着在被拒絕的任何事后()現在要去在錯誤上斷言。這可以讓你做一些很酷的事情:

return expect(foo()).to.eventually
  .be.rejectedWith("my error message")
  .and.be.an.instanceOf(Error)
  .and.have.property('code', 'EFOO');

Some of the chai methods also chain, so you can use that to make some quite deeply nested assertions about the error:

一些chai方法也鏈,所以你可以使用它來做一些關於錯誤的非常深層嵌套的斷言:

return expect(foo()).to.eventually
  .be.rejectedWith("my error message")
  .and.have.property('stack')
    .that.includes('myfile.js:30')

#2


30  

Having version 5.1.0 of ChaiAsPromised, solution from Keithamus did not work for me - rejectedWith did not gave me the error object to assert, but "rejected" did:

擁有ChaiAsPromised的5.1.0版本,來自Keithamus的解決方案對我不起作用 - 被拒絕但沒有給我斷言的錯誤對象,但是“拒絕”做了:

return expect(foo())
    .to.be.rejected
    .and.be.an.instanceOf(Error)
    .and.have.property('code', 'EFOO');

For asserting multiple properties

用於聲明多個屬性

return expect(foo())
    .to.be.rejected
    .then(function(error) {
        expect(error).to.have.property('name', 'my error message');
        expect(error).to.have.property('code', 'EFOO');
    });

#3


2  

@Markko Paas's solution didn't work for me until I added 'eventually', or else rejected value is always {} empty object.

@Markko Paas的解決方案對我來說不起作用,直到我添加'最終',否則拒絕值總是{}空對象。

return expect(foo())
    .to.eventually.be.rejected
    .and.be.an.instanceOf(Error)
    .and.have.property('code', 'EFOO');

#4


0  

You can perform complex tests on errors using rejected.then:

您可以使用rejected.then對錯誤執行復雜的測試:

it('throws a complex error', function () {
  return expect(foo()).to.eventually.be.rejected.then((error) => {
    expect(error.code).to.equal('expected code');
    // other tests
    // alternatively,
    expect (error).to.eql({
      foo: 'foo',
      bar: 'bar
    });
  });
});

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/03/31/720c912c0433cfabc7ab9fcbbbcde54.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com