当尝试测试我的控制器时,Karma失败并出现一系列错误,所有错误都始于:
Karma - 错误:[$ injector:unpr]未知提供者:menuFactoryProvider < - menuFactory
似乎menuFactory(现在实际上是一个服务)没有正确注入,但我无法弄清楚原因。为清晰起见,此处显示了Karma输出:
这是我的menucontroller-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 $19.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];
};
}])
答案 0 :(得分:1)
您在注入模块之后意外关闭了描述方法,这就是为什么它无法注入您的服务。它现在有效!
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();
});
...
});
});