注入器已经创建。无法注册模块。

14 浏览
0 Comments

注入器已经创建。无法注册模块。

我是一个对Angular JS还不熟悉的新手,尝试以适当的TDD方式利用它来实现一些功能,但在测试时遇到了这个错误:

已经创建了注入器,无法注册模块!

这是我所说的服务。

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('第一次应该得到所有', 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('由于已经缓存,应该得到所有', function() {
            authorService.allAuthors = [{id:1 , name:'Prayas'}, {id:2 , name:'Prateek'}];
            var promise = authorService.getAll();
            promise.then(function(data){
                expect(data.length).toBe(2)
            });
        });
    });
})

任何帮助将不胜感激。

0