인젝터가 이미 생성되었습니다.모듈을 등록할 수 없습니다.
저는 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('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)이 에러가 발생합니다.
모든 문의처:module('someApp')를 호출하기 전에 실행해야 합니다.inject($someDependency).
주입 기능을 잘못 사용하고 있습니다.설명서에서 설명한 바와 같이 주입 함수는 $injector의 새 인스턴스를 이미 인스턴스화합니다.$injector를 인수로 injector 함수에 전달함으로써 $injector 서비스를 두 번 인스턴스화하도록 요구하는 것 같습니다.
확인하고 싶은 서비스를 전달하려면 주입을 사용하십시오.커버 아래에서는 인젝터가 인스턴스화한 $injector 서비스를 사용하여 서비스를 가져옵니다.
이 문제를 해결하려면 두 번째 앞의 문장을 다음과 같이 변경합니다.
beforeEach(inject(function(_authorService_) {
    authorService = _authorService_;
}));
 
한 가지 더 주의할 것이 있습니다.주입 함수에 전달된 인수 authorService는 '_'로 둘러싸여 있으므로 해당 인수 authorService의 이름은 descript 함수 내에 생성된 변수를 숨기지 않습니다.이는 주입 문서에도 기재되어 있습니다.
이것이 원인인지는 잘 모르겠지만, 각각의 원인은 다음과 같습니다.
beforeEach(function() {
  inject(function($injector) {
    authorService = $injector.get('authorService');
  }
});
언급URL : https://stackoverflow.com/questions/24900067/injector-already-created-can-not-register-a-module
'programing' 카테고리의 다른 글
| 배포 시 Amazon Beanstalk 오류 (0) | 2023.03.31 | 
|---|---|
| 유니코드 문자를 표시/복호화하는 Javascript 대응 (0) | 2023.03.31 | 
| 데이터베이스에서 스프링 부팅 앱 속성 로드 (0) | 2023.03.31 | 
| Facebook을 WordPress 등록/로그인에 통합하는 방법 (0) | 2023.03.31 | 
| React에 필요한 프로포즈를 1개 이상 포함 (0) | 2023.03.31 |