我是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)
您对的所有呼叫module('someApp')必须在呼叫之前发生 inject($someDependency)。