如何使用 Jasmine 测试 AngularJS 服务? [英] How do I test an AngularJS service with Jasmine?

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

问题描述

(这里有一个相关问题:Jasmine 测试没有看到 AngularJS 模块)

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

我只想在不引导 Angular 的情况下测试服务.

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.

我只有三个文件:

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

  • myService.js: where I define an AngularJS service

test_myService.js:我在其中为服务定义 Jasmine 测试.

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

specRunner.html:具有普通 jasmine 配置的 HTML 文件以及我在其中导入前两个其他文件以及 Jasmine、Angularjs 和 angular-mocks.js.

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

});

当我尝试单独测试服务时,我应该能够访问它并检查他们的方法.我的问题是:如何在不引导 AngularJS 的情况下在我的测试中注入服务?

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?

例如,如何使用 Jasmine 测试服务方法的返回值,如下所示:

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).

为了实例化服务angular.injector 必须被调用使用定义我们服务的模块.然后,我们可以向服务请求新的注入器对象,并且只有在服务最终实例化时才会请求它.

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.

类似这样的东西:

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);
        })

    })

});

另一种方法是使用invoke"将服务传递给函数:

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 );
        })

    })

});

最后,正确"的方法是使用 'inject' 和 'module''beforeEach' 茉莉花块.执行此操作时,我们必须意识到注入"功能不在标准的 angularjs 包中,而是在 ngMock 模块中,并且仅适用于 jasmine.

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);

        }))

    })

});

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

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