chai-as-promised 测试不适用于 $q 承诺 [英] chai-as-promised tests don't work with $q promises

查看:26
本文介绍了chai-as-promised 测试不适用于 $q 承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 chai-as-promised$q 承诺一起使用业力单元测试.

I'm trying to get chai-as-promised to work with $q promises with karma unit tests.

  svc.test = function(foo){
    if (!foo){
      // return Promise.reject(new Error('foo is required'));
      return $q.reject(new Error('foo is required'));
    } else {
      // get data via ajax here
      return $q.resolve({});
    }
  };


  it.only('should error on no foo', function(){
    var resolvedValue = MyServices.test();
    $rootScope.$apply();
    return resolvedValue.should.eventually.be.rejectedWith(TypeError, 'foo is required');
  });

单元测试刚刚超时.我不确定我在这里做错了什么才能得到正确解决的承诺.使用 $q 似乎是一个问题——当我使用本机 Promise.reject() 时它工作正常.

The unit test just times out. I am not sure what I'm doing wrong here to get the promise to resolve properly. It seems to be an issue with using $q -- when I use native Promise.reject() it works fine.

我在这里提交了一张票,但似乎没有人回应:https://github.com/domenic/chai-as-promised/issues/150

I filed a ticket here, but nobody seems to be responding: https://github.com/domenic/chai-as-promised/issues/150

推荐答案

chai-as-promised 希望修改 promise 断言的方式是 transferPromiseness 方法.

The way chai-as-promised expects to modify promise assertions is transferPromiseness method.

默认情况下,Chai 作为 Promised 的断言返回的 Promise 是常规 Chai 断言对象,使用单个 then 方法扩展从输入承诺派生.例如,要改变这种行为使用更有用的糖方法输出承诺,例如找到在大多数承诺库中,您可以覆盖chaiAsPromised.transferPromiseness.

By default, the promises returned by Chai as Promised's assertions are regular Chai assertion objects, extended with a single then method derived from the input promise. To change this behavior, for instance to output a promise with more useful sugar methods such as are found in most promise libraries, you can override chaiAsPromised.transferPromiseness.

对于 Angular 1.3+ 支持,$q 承诺可以通过 $$state 属性进行鸭式输入,因此原生承诺不会受到影响:

For Angular 1.3+ support, $q promises can be duck-typed by $$state property, so native promises won't be affected:

chaiAsPromised.transferPromiseness = function (assertion, promise) {
  assertion.then = promise.then.bind(promise);

  if (!('$$state' in promise))
    return;

  inject(function ($rootScope) {
    if (!$rootScope.$$phase)
      $rootScope.$digest();
  });
};

chaiAsPromisedthen 链接每个断言的承诺.即使承诺得到解决,链的其余部分仍然需要使用 $rootScope.$digest() 手动触发摘要.

chaiAsPromised chains each asserted promise with then. Even if the promise is settled, the rest of the chain still requires the digest to be triggered manually with $rootScope.$digest().

只要规范不包含异步代码,它就变成同步的,不需要返回承诺:

As long as the spec contains no asynchronous code, it becomes synchronous, no promise is required to be returned:

it('...', () => {
  ...
  expect(...).to.eventually...;
  expect(...).to.eventually...;
});

transferPromiseness未设置时,在每组最终断言/期望之后等于强制$rootScope.$digest():

And is equal to mandatory $rootScope.$digest() after each set of eventually assertions/expectation when transferPromiseness wasn't set:

it('...', () => {
  ...
  expect(...).to.eventually...;
  expect(...).to.eventually...;
  $rootScope.$digest();
});

这篇关于chai-as-promised 测试不适用于 $q 承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆