How do you mock out a simple Factory in AngularJs that returns static data in a Karma
unit test?
I have this simple factory, that for the sake of example return static data:
angular.module('conciergeApp.services')
.factory('CurrentUser', function() {
return {
id: 1,
hotel_id: 1,
}
});
I'd like to know how to write a Karma
test for this?
So far, I have this code, but id doesn't work:
describe('ReplyCtrl', function() {
beforeEach(module('conciergeApp'));
beforeEach(module(function($provide) {
$provide.service('CurrentUser', function() {
return 1;
});
}));
//Getting reference of the mocked service
var mockUtilSvc;
inject(function(CurrentUser) {
mockUtilSvc = CurrentUser;
});
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('ReplyCtrl', {
$scope: scope
});
}));
it('should return value from mock dependency', inject(function(mockUtilSvc) {
expect(mockUtilSvc()).toEqual(1);
}));
});
This is the error message that I've been getting:
Firefox 41.0.0 (Mac OS X 10.10.0) ReplyCtrl should return value from mock dependency FAILED
Error: [$injector:unpr] Unknown provider: mockUtilSvcProvider <- mockUtilSvc