Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何测试使用承诺的角度工厂函数是否在jasmine中抛出错误_Javascript_Angularjs_Jasmine_Promise - Fatal编程技术网

Javascript 如何测试使用承诺的角度工厂函数是否在jasmine中抛出错误

Javascript 如何测试使用承诺的角度工厂函数是否在jasmine中抛出错误,javascript,angularjs,jasmine,promise,Javascript,Angularjs,Jasmine,Promise,下面是我的函数的样子 var myFunc = function(){ return functionReturningaPromise() .then(function(){ //success, doesn't matter what happens here }) .catch(function(err){ //handle error here and then th

下面是我的函数的样子

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');
          })
}
我需要函数以这种方式处理函数内部的错误,然后抛出一个错误以在更高级别上处理。但我不知道如何用茉莉来测试它。我知道如何控制测试承诺,我的基本设置如下所示:

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没有发现这个错误。因此,如何在这种情况下测试错误抛出

$q
与本机
抛出
不兼容,您应该使用
$q
API在承诺链内重新抛出或创建新错误。关于这方面的一些问答:

解决方案是使用
返回$q.reject('Error in my function')
而不是
抛出新错误('Error in my function')

但悬而未决的问题是如何测试它。基本上,您可以使用承诺链,在测试中再添加一个
.catch()
,以检查错误,测试使用:


(打开侧栏中的
script.js
file)

#Michael Radionov我有一个类似的问题,您的解决方案也有效,但我担心会出现误报:
function a(){try{B()}catch(error){$q.reject(error);}//一切正常$q.resolve();function B(){throw({myError:{error:{error\u message')}}
奇怪的是,通过按原样应用您的解决方案,我得到了以下错误:
预期{myError:{error:{error:{error\u message'}}为{myError:{error:{error:{error\u message'}
但最终,当我将
$rootScope.$digest
行移到
promise.catch等
部分之前时,一切都会正常运行。关于错误消息-
toBe
matcher通过引用比较对象,您应该使用
toEqual
来执行深度相等比较。这是因为您将错误用作对象与在我的示例中,错误以字符串形式出现。但我不确定摘要是否正确,如果您在中共享了更新的代码,那就太好了。对于之前复杂的评论,我深表歉意,并提前感谢您的帮助。在第一个示例中,您在添加catch处理程序之前调用了
$rootScope.$digest
,因此整个承诺将在前面执行n达到你的目的-因此,测试没有预期并成功执行。在第二个示例中,顺序是正确的,错误抛出很好,但预期失败,因为我上面解释了
toBe
vs
toEqual
的差异。另外,确保使用Jasmine
done
对sp进行异步测试ot误报,因为它将显式地等待异步期望。您应该检查Jasmine 1.x文档中的部分,它比
done
稍微复杂一些,但可以完成任务。关键是您有一个自定义
标志,默认情况下为false,Jasmine
等待它在一些异步操作后变为true然后,在它变为真后,Jasmine执行最后一个
运行
块。
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();
});