Javascript 喷油器已创建。无法注册模块
我是Angular JS的新手,试图以适当的TDD方式从中获得一些东西,但在测试过程中,我遇到了以下错误: 喷油器已创建,无法注册模块 这就是我所说的服务。Javascript 喷油器已创建。无法注册模块,javascript,angularjs,Javascript,Angularjs,我是Angular JS的新手,试图以适当的TDD方式从中获得一些东西,但在测试过程中,我遇到了以下错误: 喷油器已创建,无法注册模块 这就是我所说的服务。 bookCatalogApp.service('authorService', ["$resource", "$q", function($resource, $q){ var Author =$resource('/book-catalog/author/all',{},{ getAll : { method:
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)
});
});
});
})
任何帮助都将不胜感激。不确定这是否是原因,但您的前任应该是这样的:
beforeEach(function() {
inject(function($injector) {
authorService = $injector.get('authorService');
}
});
您使用的注入函数错误。如前所述,inject函数已经实例化了$injector的一个新实例。我的猜测是,通过将$injector作为参数传递给injector函数,您要求它实例化$injector服务两次
只需使用inject传入您要检查的服务。在封面下,inject将使用它实例化的$injector服务获取服务
您可以通过将每个语句之前的第二个语句更改为:
beforeEach(inject(function(_authorService_) {
authorService = _authorService_;
}));
还有一件事需要注意。传递给inject函数的参数authorService已用“\”包装,因此其名称不会隐藏在descripe函数中创建的变量。这也记录在inject中。如果混合调用
模块('someApp')
和inject($someDependency)
,则会出现此错误
所有对
模块('someApp')
的调用必须在调用注入($someDependency)
之前进行。将$injector传递到注入函数中的可能重复项是多余的,并且是在这种情况下导致问题的原因。这是注入服务的完全合法的方式。可能发生的情况是,在设置测试时,在使用mocks.inject之后,无法使用$provide.value范例。事实上,即使mocks.inject语句涉及不相关的模块,也不能在任何mocks.inject语句之后使用$provide.value。