Encountering issues while testing my controller with Karma, I am faced with a series of errors all originating from:
Karma - Error: [$injector:unpr] Unknown provider: menuFactoryProvider <- menuFactory
It appears that menuFactory (now a service) is not being injected correctly, but the reason eludes me. For clarity, here is the output from Karma:
https://i.sstatic.net/EepCn.png
Below is an excerpt from my 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();
});
...
});
Excerpt from 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;
});
Excerpt from services.js (note again menuFactory is actually a service, not a factory)
'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];
};
}])