Karma - Unknown provider error: menuFactoryProvider <- menuFactory
Karma - Unknown provider error: menuFactoryProvider <- menuFactory
当尝试测试我的控制器时,Karma 失败并出现一串错误,所有错误都以:
Karma - Error: [$injector:unpr] Unknown provider: menuFactoryProvider
<- menuFactory
menuFactory(现在实际上是一个服务)似乎没有正确注入,但我不明白为什么。为清楚起见,此处显示业力输出:
这是我的菜单控制器-test.js:
describe('Controller: MenuController', function () {
// load the controller's module
beforeEach(module('confusionApp'));
var MenuController, scope, $httpBackend;
});
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, _$httpBackend_, $rootScope, menuFactory) {
// place here mocked dependencies
$httpBackend = _$httpBackend_;
$httpBackend.expectGET("http://localhost:3000/dishes").respond([
{
"id": 0,
...
},
{
"id": 1,
...
}
]);
scope = $rootScope.$new();
MenuController = $controller('MenuController', {
$scope: scope, menuFactory: menuFactory
});
$httpBackend.flush();
}));
it('should have showDetails as false', function () {
expect(scope.showDetails).toBeFalsy();
});
...
});
摘自controllers.js
'use strict';
angular.module('confusionApp')
.controller('MenuController', ['$scope', 'menuFactory', function($scope, menuFactory) {
$scope.tab = 1;
$scope.filtText = '';
$scope.showDetails = false;
$scope.showMenu = false;
$scope.message = "Loading ...";
menuFactory.getDishes().query(
function(response) {
$scope.dishes = response;
$scope.showMenu = true;
},
function(response) {
$scope.message = "Error: "+response.status + " " + response.statusText;
});
摘自services.js(再次注意menuFactory实际上是服务,而不是工厂)
'use strict';
angular.module('confusionApp')
.constant("baseURL", "http://localhost:3000/")
.service('menuFactory', ['$resource', 'baseURL', function($resource, baseURL) {
var promotions = [
{
_id:0,
name:'Weekend Grand Buffet',
image: 'images/buffet.png',
label:'New',
price:'19.99',
description:'Featuring mouthwatering combinations with a choice of five different salads, six enticing appetizers, six main entrees and five choicest desserts. Free flowing bubbly and soft drinks. All for just .99 per person ',
}
];
this.getDishes = function(){
return $resource(baseURL+"dishes/:id",null, {'update':{method:'PUT' }});
};
// implement a function named getPromotion
// that returns a selected promotion.
this.getPromotion = function(index) {
return promotions[index];
};
}])
您在注入模块后不小心关闭了 describe 方法,这就是它无法注入您的服务的原因。现在可以使用了!
describe('Controller: MenuController', function () {
// load the controller's module
beforeEach(module('confusionApp'));
var MenuController, scope, $httpBackend,menuFactory;
// Initialize the controller and a mock scope
beforeEach(inject(function ($injector,$controller, _$httpBackend_, $rootScope, _menuFactory_) {
// place here mocked dependencies
$httpBackend = _$httpBackend_;
menuFactory = $injector.get('menuFactory');
$httpBackend.expectGET("http://localhost:3000/dishes").respond([
{
"id": 0,
...
},
{
"id": 1,
...
}
]);
scope = $rootScope.$new();
MenuController = $controller('MenuController', {
$scope: scope, menuFactory: menuFactory
});
$httpBackend.flush();
}));
it('should have showDetails as false', function () {
expect(scope.showDetails).toBeFalsy();
});
...
});
});
当尝试测试我的控制器时,Karma 失败并出现一串错误,所有错误都以:
Karma - Error: [$injector:unpr] Unknown provider: menuFactoryProvider <- menuFactory
menuFactory(现在实际上是一个服务)似乎没有正确注入,但我不明白为什么。为清楚起见,此处显示业力输出:
这是我的菜单控制器-test.js:
describe('Controller: MenuController', function () {
// load the controller's module
beforeEach(module('confusionApp'));
var MenuController, scope, $httpBackend;
});
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, _$httpBackend_, $rootScope, menuFactory) {
// place here mocked dependencies
$httpBackend = _$httpBackend_;
$httpBackend.expectGET("http://localhost:3000/dishes").respond([
{
"id": 0,
...
},
{
"id": 1,
...
}
]);
scope = $rootScope.$new();
MenuController = $controller('MenuController', {
$scope: scope, menuFactory: menuFactory
});
$httpBackend.flush();
}));
it('should have showDetails as false', function () {
expect(scope.showDetails).toBeFalsy();
});
...
});
摘自controllers.js
'use strict';
angular.module('confusionApp')
.controller('MenuController', ['$scope', 'menuFactory', function($scope, menuFactory) {
$scope.tab = 1;
$scope.filtText = '';
$scope.showDetails = false;
$scope.showMenu = false;
$scope.message = "Loading ...";
menuFactory.getDishes().query(
function(response) {
$scope.dishes = response;
$scope.showMenu = true;
},
function(response) {
$scope.message = "Error: "+response.status + " " + response.statusText;
});
摘自services.js(再次注意menuFactory实际上是服务,而不是工厂)
'use strict';
angular.module('confusionApp')
.constant("baseURL", "http://localhost:3000/")
.service('menuFactory', ['$resource', 'baseURL', function($resource, baseURL) {
var promotions = [
{
_id:0,
name:'Weekend Grand Buffet',
image: 'images/buffet.png',
label:'New',
price:'19.99',
description:'Featuring mouthwatering combinations with a choice of five different salads, six enticing appetizers, six main entrees and five choicest desserts. Free flowing bubbly and soft drinks. All for just .99 per person ',
}
];
this.getDishes = function(){
return $resource(baseURL+"dishes/:id",null, {'update':{method:'PUT' }});
};
// implement a function named getPromotion
// that returns a selected promotion.
this.getPromotion = function(index) {
return promotions[index];
};
}])
您在注入模块后不小心关闭了 describe 方法,这就是它无法注入您的服务的原因。现在可以使用了!
describe('Controller: MenuController', function () {
// load the controller's module
beforeEach(module('confusionApp'));
var MenuController, scope, $httpBackend,menuFactory;
// Initialize the controller and a mock scope
beforeEach(inject(function ($injector,$controller, _$httpBackend_, $rootScope, _menuFactory_) {
// place here mocked dependencies
$httpBackend = _$httpBackend_;
menuFactory = $injector.get('menuFactory');
$httpBackend.expectGET("http://localhost:3000/dishes").respond([
{
"id": 0,
...
},
{
"id": 1,
...
}
]);
scope = $rootScope.$new();
MenuController = $controller('MenuController', {
$scope: scope, menuFactory: menuFactory
});
$httpBackend.flush();
}));
it('should have showDetails as false', function () {
expect(scope.showDetails).toBeFalsy();
});
...
});
});