注入器已经创建.无法注册模块 [英] injector already created. can not register a module

查看:18
本文介绍了注入器已经创建.无法注册模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Angular JS 的新手,并试图以适当的 TDD 方式利用它来做点什么,但是在测试时我遇到了这个错误:

I am a new bee to Angular JS and was trying to make something out of it in a proper TDD way, but while testing i am getting this error:

Injector 已经创建,不能注册模块!

Injector already created, can not register a module!

这就是我所说的服务.

bookCatalogApp.service('authorService', ["$resource", "$q", function($resource, $q){

    var Author =$resource('/book-catalog/author/all',{},{
        getAll : { method: 'GET', isArray: true}
    });

    var authorService = {};

    authorService.assignAuthors = function(data){
        authorService.allAuthors = data;
    };

    authorService.getAll = function(){
        if (authorService.allAuthors)
            return {then: function(callback){callback(authorService.allAuthors)}}

        var deferred = $q.defer();
        Author.getAll(function(data){
            deferred.resolve(data);
            authorService.assignAuthors(data);
        });
        return deferred.promise;
    };

    return authorService;
}]);

这是对上述服务的测试

describe("Author Book Service",function(){
    var authorService;
    beforeEach(module("bookCatalogApp"));
    beforeEach(inject(function($injector) {
        authorService = $injector.get('authorService');
    }));

    afterEach(function() {
        httpBackend.verifyNoOutstandingExpectation();
        httpBackend.verifyNoOutstandingRequest();
    });

    describe("#getAll", function() {
        it('should get all the authors for the first time', function() {
            var authors = [{id:1 , name:'Prayas'}, {id:2 , name:'Prateek'}];

            httpBackend.when('GET', '/book-catalog/author/all').respond(200, authors);

            var promise = authorService.getAll();

            httpBackend.flush();
            promise.then(function(data){
                expect(data.length).toBe(2)
            });
        });

        it('should get all the authors as they have already cached', function() {
            authorService.allAuthors = [{id:1 , name:'Prayas'}, {id:2 , name:'Prateek'}];

            var promise = authorService.getAll();

            promise.then(function(data){
                expect(data.length).toBe(2)
            });
        });

    });
})

任何帮助将不胜感激.

推荐答案

如果你混合调用 module('someApp')inject($someDependency) 你会得到这个错误.

If you are mixing calls to module('someApp') and inject($someDependency) you will get this error.

您对 module('someApp') 的所有调用都必须在您对 inject($someDependency) 的调用之前发生.

All your calls to module('someApp') must occur before your calls to inject($someDependency).

这篇关于注入器已经创建.无法注册模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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