在与AngularJS应用程序运行加载的东西单元测试时, [英] Unit test when loading things at app run with AngularJS

查看:89
本文介绍了在与AngularJS应用程序运行加载的东西单元测试时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的应用程序在运行时VI运行一些配置HTTP端点。

我写了一个简单的服务要做到这一点:

  module.factory('配置',函数($ HTTP,分析){
    返回{
        载:函数(){
            $ http.get('/配置'),然后(功能(响应){
                analytics.setAccount(response.googleAnalyticsAccount);
            });
        }
    }
});

接下来,我把这个模块在我的应用程序模块的运行段:

  angular.module(应用)。***。运行(功能(配置){
        config.load();
    });

一切工作正常应用程序运行时,但在我的单元测试,我得到这个错误:错误:意外的请求:GET /配置

我知道这意味着什么,但我不知道如何当它从一个运行程序段发生嘲笑它。

感谢您的帮助。

编辑添加的规范

调用此之前,每一个

  beforeEach(angular.mock.module('应用'));

试过这个来嘲笑$ httpBackend:

  beforeEach(注(函数($ httpBackend){    $ httpBackend.expectGET('/配置)响应(200,{'googleAnalyticsAccount':})。    angular.mock.module(应用)    $ httpBackend.flush();
}));

不过的了:

 类型错误:无法读取空的财产栈
        在workFn(/Users/arnaud/workspace/unishared-dredit/test/lib/angular/angular-mocks.js:1756:55)
    类型错误:无法读取空的财产栈
        在workFn(/Users/arnaud/workspace/unishared-dredit/test/lib/angular/angular-mocks.js:1756:55)
    类型错误:无法读取空的财产栈
        在workFn(/Users/arnaud/workspace/unishared-dredit/test/lib/angular/angular-mocks.js:1756:55)

由于更新AngularJS 1.0.6修改

自从我更新到1.0.6 AngularJS从角队由Igor建议,这个问题已经一去不复返了,但现在我现在得到这个,这听起来更正常,但我仍然无法弄清楚如何使它工作。

 错误:已创建的喷油器,无法注册模块!


解决方案

我这个错误一小会儿挣扎,但设法拿出一个合理的解决方案。

我想实现的是成功的存根服务并强制响应,控制器上它启动之前,是可以使用 $ httpBackend 与请求存根或异常控制器。
app.run()当加载模块它初始化对象和它的相关的服务等。

我设法使用下面的示例存根服务。

 描述('测试应用程序运行',函数(){
    beforeEach(模块('plunker',函数($提供){
        返回$ provide.decorator('配置',函数(){
            返回{
                载:函数(){
                    返回{};
                }
            };
        });
    }));
    变量$ rootScope;
    beforeEach(注(功能(_ $ rootScope_){
        返回$ rootScope = _ $ rootScope_;
    }));    它(定义值I previously无法测试功能(){
        预期回报率($ rootScope.value).toEqual('测试');
    });});

我希望这有助于您在未来的 app.run()测试。

I need my app to run some configuration at runtime vi an HTTP endpoint.

I wrote a simple service to do that:

module.factory('config', function ($http, analytics) {
    return {
        load: function () {
            $http.get('/config').then(function (response) {
                analytics.setAccount(response.googleAnalyticsAccount);
            });
        }
    }
});

Next, I call this module in a run block of my app module:

angular.module('app').***.run(function(config) {
        config.load();
    });

All is working well when the app is running but in my unit tests, I get this error: "Error: Unexpected request: GET /config"

I know what it means but I don't know how to mock it when it happens from a run block.

Thanks for your help

EDIT to add spec

Calling this before each

beforeEach(angular.mock.module('app'));

Tried this to mock $httpBackend:

beforeEach(inject(function($httpBackend) {

    $httpBackend.expectGET('/config').respond(200, {'googleAnalyticsAccount':});

    angular.mock.module('app')

    $httpBackend.flush();
}));

But got:

TypeError: Cannot read property 'stack' of null
        at workFn (/Users/arnaud/workspace/unishared-dredit/test/lib/angular/angular-mocks.js:1756:55)
    TypeError: Cannot read property 'stack' of null
        at workFn (/Users/arnaud/workspace/unishared-dredit/test/lib/angular/angular-mocks.js:1756:55)
    TypeError: Cannot read property 'stack' of null
        at workFn (/Users/arnaud/workspace/unishared-dredit/test/lib/angular/angular-mocks.js:1756:55)

EDIT since update to AngularJS 1.0.6

Since I've updated to AngularJS 1.0.6, advised by Igor from the Angular team, the issue is gone but now I've now got this one, which sounds more "normal" but I still can't figure out how to make it works.

Error: Injector already created, can not register a module!

解决方案

I struggled with this error for a little while, but managed to come up with an sensible solution.

What I wanted to achieve is to successfully stub the Service and force a response, on controllers it was possible to use $httpBackend with a request stub or exception before initiating the controller. In app.run() when you load the module it initialises the object and it's connected Services etc.

I managed to stub the Service using the following example.

describe('Testing App Run', function () {
    beforeEach(module('plunker', function ($provide) {
        return $provide.decorator('config', function () {
            return {
                load: function () {
                    return {};
                }
            };
        });
    }));


    var $rootScope;
    beforeEach(inject(function (_$rootScope_) {
        return $rootScope = _$rootScope_;
    }));

    it("defines a value I previously could not test", function () {
        return expect($rootScope.value).toEqual('testing');
    });

});

I hope this helps your app.run() testing in the future.

这篇关于在与AngularJS应用程序运行加载的东西单元测试时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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