Javascript 按照承诺用 Chai 测试拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29363455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:15:44  来源:igfitidea点击:

Test a rejection with Chai as promised

javascriptchai

提问by Guid

I want to test a function returning a promise.

我想测试一个返回承诺的函数。

In this particular test, the promise is expected to be rejected with an Error object containing the classical messagefield (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")

在这个特定的测试中,promise 预计会被一个包含经典message字段的 Error 对象(在这个测试中,它应该等于"my error message")和一个我添加的自定义字段命名为code,它是一个字符串(如“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 codefield too.
How to do that?

这个断言是有效的,但现在我也想测试这个code领域。
怎么做?

回答by Keithamus

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(正如您所说的那样),那么它允许链接rejectedWith- 并将链断言对象设置为错误对象 - 意味着之后的任何rejectedWith()内容现在都将在错误上断言. 这让你可以做很酷的事情,比如:

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')

回答by Markko Paas

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 的解决方案对我不起作用-rejectedWith 没有给我断言的错误对象,但“拒绝”做了:

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');
    });

回答by SifouTifou

@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');

回答by Rich Apodaca

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
    });
  });
});