单元测试angularjs异步服务 [英] Unit testing an asynchronous service in angularjs

查看:171
本文介绍了单元测试angularjs异步服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想进行单元测试有异步方法服务,但我有没有运气。

我曾尝试通过angularjs使用$ Q支持,承诺实现。

任何帮助将是AP preciated。

http://jsfiddle.net/9pBze/37/

  angular.module('MyApp的',['为MyService']);angular.module('为MyService,[])。工厂('为MyService',函数($ Q){
  变种LS = {};  ls.DoIt =功能(){
    变种推迟= $ q.defer();    的setTimeout(函数(){
        deferred.resolve(5);
    },3000);    返回deferred.promise;
  }  返回LS;});描述('服务',函数(){    beforeEach(模块('为MyService'));    它(应该等于2,注(功能(为MyService){
        myservice.DoIt()。然后(功能(返回){
            期待(返回).toEqual(2);
        });
    }));
});


解决方案

首先,在的setTimeout 是特别棘手的测试,因为它很难嘲笑。幸运的是AngularJS周围有( $超时)的包装,发挥同样的作用,但可以很容易地嘲笑:

  ls.DoIt =功能(){
    变种推迟= $ q.defer();    $超时(函数(){
        deferred.resolve(5);
    },3000);    返回deferred.promise;
  }

提供了 $超时的模拟使我们能够轻松地模拟运行时间(以 $ timeout.flush() ),这意味着我们的测试能跑多快,没有真正等待异步事件来完成(请注意,生产code仍在使用异步API!)。

该改变的测试看起来像:

 它(应等于5,注入(功能(为MyService,$超时){    VAR valueToVerify;
    myservice.DoIt()。然后(功能(返回){
      valueToVerify =返回;
    });
    $ timeout.flush();
    期待(valueToVerify).toEqual(5);
}));

最后工作的jsfiddle: http://jsfiddle.net/v9L9G/1/

I am trying to unit test a service which has asynchronous methods but am having no luck.

I have tried to implement with promises by using the $q support in angularjs.

Any help would be appreciated.

http://jsfiddle.net/9pBze/37/

angular.module('myapp', ['myservice']);

angular.module('myservice', []).factory('myservice', function($q) {
  var ls = {};

  ls.DoIt = function() {
    var deferred = $q.defer();

    setTimeout(function(){
        deferred.resolve(5);
    },3000);

    return deferred.promise;
  }

  return ls;

});

describe('services', function () {

    beforeEach(module('myservice'));

    it("should equal 2",  inject(function(myservice) {
        myservice.DoIt().then(function(returned) {
            expect(returned).toEqual(2);
        });        
    }));
});

解决方案

First of all, the setTimeout is particularly tricky to test since it hard to mock. Fortunately AngularJS has a wrapper around it ($timeout) that plays the same role but can be easily mocked:

  ls.DoIt = function() {
    var deferred = $q.defer();

    $timeout(function(){
        deferred.resolve(5);
    },3000);

    return deferred.promise;
  }

The mock provided for $timeout allows us to easily simulate elapsed time (with $timeout.flush()) which means our tests can run fast, without really waiting for the async event to complete (please note that the production code is still using async API!).

The changed tests would look like:

it("should equal 5",  inject(function(myservice, $timeout) {

    var valueToVerify;
    myservice.DoIt().then(function(returned) {
      valueToVerify = returned;  
    });  
    $timeout.flush();        
    expect(valueToVerify).toEqual(5);
}));

And finally the working jsFiddle: http://jsfiddle.net/v9L9G/1/

这篇关于单元测试angularjs异步服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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