AngularJS:注入测试中导致错误 [英] AngularJS: inject causing error in tests

查看:130
本文介绍了AngularJS:注入测试中导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法用在我的测试中注入

两者角嘲笑版本1.3.14

我完全失去了这里。任何想法将大大AP preciated!

这将导致一个错误:

 描述(工厂:帐户功能(){    变量$ httpBackend;
    变量$ rootScope;
    VAR账户;    beforeEach(模块(应用));    beforeEach(注(函数(){
        // $ httpBackend = _ $ httpBackend_;
    }));
    它(应该获取账号功能(){
    });
});

但是,如果我删除注入它会通过:

 描述(工厂:帐户功能(){    变量$ httpBackend;
    变量$ rootScope;
    VAR账户;    beforeEach(模块(应用));    beforeEach(函数(){
        // $ httpBackend = _ $ httpBackend_;
    });
    它(应该获取账号功能(){
    });
});

在Gruntfile.js:

 因果报应:{
        单位:{
            选项​​:{
                框架:['茉莉'],
                singleRun:真实,
                浏览器:['野生'],
                文件:
                    bower_components /角/ angular.js',
                    bower_components /角嘲笑/角mocks.js',
                    bower_components /力矩/ moment.js',
                    bower_components / CKEditor的/ ckeditor.js',
                    bower_components / NG-的CKEditor / NG-ckeditor.min.js',
                    应用程序/ app.js,
                    应用程序/ ** / *。JS',
                ]
            }
        }
    }

下面是测试的输出:

 的Safari 8.0.5(Mac OS X的10.10.3)工厂:帐户应获取帐户失败
    /.../bower_components/angular/angular.js:63:32
    /.../bower_components/angular/angular.js:4120:30
    @的forEach / ... / bower_components /角/ angular.js:323:24
    loadModules @ / ... / bower_components /角/ angular.js:4081:12
    createInjector @ / ... / bower_components /角/ angular.js:4007:22
    workFn @ / ... / bower_components /角嘲笑/角mocks.js:2353:60


解决方案

我觉得你应该这样写:

 描述(工厂:帐户功能(){
    变量$ httpBackend;
    变量$ rootScope;
    VAR账户;    beforeEach(模块(应用));    beforeEach(功能(_ $ httpBackend_){
         $ httpBackend = _ $ httpBackend_;
    });    它(应该获取账号功能(){
    });
});

您使用前需要先声明 $ httpBackend

I am unable to use inject in my tests.

Both angular and angular-mocks are version 1.3.14

I am completely lost here. Any ideas will be greatly appreciated!

This causes an error:

describe("factory: Account", function () {

    var $httpBackend;
    var $rootScope;
    var Account;

    beforeEach(module('app'));

    beforeEach(inject(function() {
        // $httpBackend = _$httpBackend_;
    }));


    it("Should fetch account", function() {
    });
});

But if I remove inject it will pass:

describe("factory: Account", function () {

    var $httpBackend;
    var $rootScope;
    var Account;

    beforeEach(module('app'));

    beforeEach(function() {
        // $httpBackend = _$httpBackend_;
    });


    it("Should fetch account", function() {
    });
});

In Gruntfile.js:

    karma: {
        unit: {
            options: {
                frameworks: ['jasmine'],
                singleRun: true,
                browsers: ['Safari'],
                files: [
                    'bower_components/angular/angular.js',
                    'bower_components/angular-mocks/angular-mocks.js',
                    'bower_components/moment/moment.js',
                    'bower_components/ckeditor/ckeditor.js',
                    'bower_components/ng-ckeditor/ng-ckeditor.min.js',
                    'app/app.js',
                    'app/**/*.js',
                ]
            }
        }
    }

Here is the output of the test:

Safari 8.0.5 (Mac OS X 10.10.3) factory: Account Should fetch account FAILED
    /.../bower_components/angular/angular.js:63:32
    /.../bower_components/angular/angular.js:4120:30
    forEach@/.../bower_components/angular/angular.js:323:24
    loadModules@/.../bower_components/angular/angular.js:4081:12
    createInjector@/.../bower_components/angular/angular.js:4007:22
    workFn@/.../bower_components/angular-mocks/angular-mocks.js:2353:60

解决方案

I think you should write it like this:

describe("factory: Account", function () {
    var $httpBackend;
    var $rootScope;
    var Account;

    beforeEach(module('app'));

    beforeEach(function(_$httpBackend_) {
         $httpBackend = _$httpBackend_;
    });

    it("Should fetch account", function() {
    });
});

You need to declare $httpBackend before using it.

这篇关于AngularJS:注入测试中导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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