如何测试与茉莉花的AngularJS服务? [英] How do I test an AngularJS service with Jasmine?

查看:138
本文介绍了如何测试与茉莉花的AngularJS服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这里有一个相关的问题:<一href=\"http://stackoverflow.com/questions/12938388/jasmine-test-does-not-see-angularjs-module\">Jasmine测试不看AngularJS模块)

我只是想不自引导角度来测试服务。

我有看一些例子和教程,但我不会去任何地方。

我刚才三个文件:


  • myService.js:在那里我定义一个AngularJS服务


  • test_myService.js:在这里我定义为服务茉莉花测试


  • specRunner.html:一个HTML文件与正常茉莉配置
    并在我导入previous其他两个文件和茉莉花,Angularjs和角mocks.js。


这是code的服务(如预期时,我没有测试,工程):

  VAR Mymodule中= angular.module('Mymodule中',[]);myModule.factory('为myService',函数(){    变种serviceImplementation = {};
    serviceImplementation.one = 1;
    serviceImplementation.two = 2;
    serviceImplementation.three = 3;    回报serviceImplementation});

由于我试图隔离测试服务,我应该能够访问它,并检查他们的方法。
我的问题是:我怎么能注入在我的测试服务,而不自引导AngularJS

比如说,我怎么可以测试服务茉莉这样的方法的返回值:

 描述('为myService测试,函数(){
    描述(当我打电话myService.one',函数(){
        它('返回1',函数(){
            MyModule的= angular.module('Mymodule中');
                    //缺少的东西在这里..
            期待(myService.one).toEqual(1);
        })    })});


解决方案

的问题是,工厂方法,即实例化服务,是不是上面(仅在创建模块不实例化服务)的例子调用。

为了向服务要实例 angular.injector 的具有与模块被调用,其中提供了服务被定义。然后,我们可以要求新的喷油器对象服务及其只有当服务最终实例化。

像这样工作的:

 描述('为myService测试,函数(){
    描述(当我打电话myService.one',函数(){
        它('返回1',函数(){
            变量$ =喷油angular.injector(['MyModule的']);
            变种为myService = $ injector.get('为myService');
            期待(myService.one).toEqual(1);
        })    })});

另一种方法是使用路过的服务功能调用':

 描述('为myService测试,函数(){
    描述(当我打电话myService.one',函数(){
        它('返回1',函数(){            myTestFunction =功能(aService){
                期待(aService.one).toEqual(1);
            }            //我们只需要以下行,如果的名字
            //在myTestFunction参数不是'为myService',或者如果
            //将code将是缩小。
            。myTestFunction $注射= ['为myService'];            VAR myInjector = angular.injector(['MyModule的']);
            myInjector.invoke(myTestFunction);
        })    })});

最后,在'适当'的方式来做到这一点是使用注射模块的在一个beforeEach 茉莉花块。
如果这样做,我们必须认识到,注入功能它不是标准angularjs包,但ngMock模块中,它只能与茉莉的作品。

 描述('为myService测试,函数(){
    描述(当我打电话myService.one',函数(){
        beforeEach(模块('Mymodule中'));
        它('回报1,注射(功能(为myService){//参数名=服务名​​称            期待(myService.one).toEqual(1);        }))    })});

(There is a related question here: Jasmine test does not see AngularJS module)

I just want to test a service without bootstrapping Angular.

I have look at some examples and the tutorial but I am not going anywhere.

I have just three files:

  • myService.js: where I define an AngularJS service

  • test_myService.js: where I define a Jasmine test for the service.

  • specRunner.html: a HTML file with the normal jasmine configuration and where I import the previous two other files and the Jasmine, Angularjs and angular-mocks.js.

This is the code for the service (that works as expected when I am not testing):

var myModule = angular.module('myModule', []);

myModule.factory('myService', function(){

    var serviceImplementation   = {};
    serviceImplementation.one   = 1;
    serviceImplementation.two   = 2;
    serviceImplementation.three = 3;

    return serviceImplementation

});

As I am trying to test the service in isolation, I should be able to access it and check their methods. My question is: how can I inject the service in my test without bootstrapping AngularJS?

For instance, how can I test the value returned for a method of the service with Jasmine like this:

describe('myService test', function(){
    describe('when I call myService.one', function(){
        it('returns 1', function(){
            myModule = angular.module('myModule');
                    //something is missing here..
            expect( myService.one ).toEqual(1);
        })

    })

});

解决方案

The problem is that the factory method, that instantiate the service, is not called in the example above (only creating the module doesn't instantiate the service).

In order to the service to be instantiated angular.injector has to be called with the module where our service is defined. Then, we can ask to the new injector object for the service and its only then when the service is finally instantiated.

Something like this works:

describe('myService test', function(){
    describe('when I call myService.one', function(){
        it('returns 1', function(){
            var $injector = angular.injector([ 'myModule' ]);
            var myService = $injector.get( 'myService' );
            expect( myService.one ).toEqual(1);
        })

    })

});

Another way would be passing the service to a function using 'invoke':

describe('myService test', function(){
    describe('when I call myService.one', function(){
        it('returns 1', function(){

            myTestFunction = function(aService){
                expect( aService.one ).toEqual(1);
            }

            //we only need the following line if the name of the 
            //parameter in myTestFunction is not 'myService' or if
            //the code is going to be minify.
            myTestFunction.$inject = [ 'myService' ];

            var myInjector = angular.injector([ 'myModule' ]);
            myInjector.invoke( myTestFunction );
        })

    })

});

And, finally, the 'proper' way to do it is using 'inject' and 'module' in a 'beforeEach' jasmine block. When doing it we have to realize that the 'inject' function it's not in the standard angularjs package, but in the ngMock module and that it only works with jasmine.

describe('myService test', function(){
    describe('when I call myService.one', function(){
        beforeEach(module('myModule'));
        it('returns 1', inject(function(myService){ //parameter name = service name

            expect( myService.one ).toEqual(1);

        }))

    })

});

这篇关于如何测试与茉莉花的AngularJS服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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