如何测试使用promise的角度工厂函数是否在jasmine中抛出错误 [英] How to test that an angular factory function using a promise is throwing an error in jasmine

查看:160
本文介绍了如何测试使用promise的角度工厂函数是否在jasmine中抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的函数的样子。

Here's how my function looks like.

var myFunc = function(){
   return functionReturningaPromise()
          .then(function(){
              //success, doesn't matter what happens here
          })
          .catch(function(err){
              //handle error here and then throw to handle higher
              throw new Error('Error in my function');
          })
}

我需要这个函数来处理这个函数内部的错误,然后抛出一个错误来处理更高级别。但我不知道如何用茉莉花测试它。我知道如何控制测试的承诺,我的基本设置如下所示:

I need the function to be this way to handle an error inside this function and then throw an error to handle on higher level. But i don't know how to test it with jasmine. I know how to control the promises for testing and my basic set up looks like this:

it('Should throw an error', inject(function(alert) {
    var instance = instanceFactory.createInstance(someData);
    var deferred = $q.defer();
    spyOn(someFactory, 'someMethod').and.returnValue(deferred.promise);

    //instance contains the throwing function above 

    instance.myFunc(otherData);

    deferred.reject({data: '12 - error'});
    $rootScope.$digest();

    expect(instance.myFunc).toThrow();

}));

显然,jasmine找不到错误。那么如何在这种情况下测试错误抛出

Obviously, the error is not found by jasmine. So how to test the error throw in this case

推荐答案

$ q 使用本机 throw 时,你应该使用 $ q API在promise中重新抛出或创建新错误链。一些Q / A来了解它:

$q does not work well with native throw, you should use $q API to re-throw or create new errors inside promise chain. Some Q/A to read about it:

  • Promise.catch() does not catch exception in AngularJS unit test
  • Why can I not throw inside a Promise.catch handler?
  • AngularJS - Promises rethrow caught exceptions

解决方案是使用返回$ q.reject('我的函数中的错误')而不是抛出新错误('我的函数错误');

The solution will be to use return $q.reject('Error in my function') instead of throw new Error('Error in my function');.

但是开放的问题是如何测试它。基本上你可以使用promise链并在test中添加一个 .catch()来检查错误,测试是使用 Jasmine Async API

But the open question is how to test it. Basically you can make use of promise chains and add one more .catch() in test to check for error, and test is using Jasmine Async API:

it('should throw an error', function (done) {
// ----- use Jasmine async API -------^^^

    var instance = instanceFactory.createInstance(someData);
    var deferred = $q.defer();
    spyOn(someFactory, 'someMethod').and.returnValue(deferred.promise);

    // here we continue catching and check the error
    var promise = instance.myFunc(otherData);
    promise.catch(function (err) {
        expect(err).toBe('Error in my function');
        done();
    });

    deferred.reject({data: '12 - error'});
    $rootScope.$digest();
});

这是一个工作样本(在侧边栏中打开 script.js 文件)

Here is a working sample (open script.js file in sidebar)

这篇关于如何测试使用promise的角度工厂函数是否在jasmine中抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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