1) You inject in the wrong place. You specify the insert function in your it test, just delete it.
i.e.
it('should return value from mock dependency', inject(function(mockUtilSvc) { expect(mockUtilSvc()).toEqual(1); }));
will become
it('should return value from mock dependency', function() { expect(mockUtilSvc()).toEqual(1); });
2) Now the problem is that mockUtilSvc is a service instance and not a function according to your definition, also based on your layout you need to define it as factory (i.e. you return 1 as the factory value and it is not capable new ).
beforeEach(module(function($provide) { $provide.factory('CurrentUser', function() { return 1;
and
expect(mockUtilSvc).toEqual(1);
3) You can also write this as:
$provide.value('CurrentUser', 1);
4) Your first inject also in the wrong place. You simply run it, but you do not start and do not assign an instance during each run of the specification. before Each iteration, you need to set the service instance value for your variable, i.e.:
beforeEach(inject(function(CurrentUser) { mockUtilSvc = CurrentUser; }));
Test example:
describe('ReplyCtrl', function() { var mockData = { id: 1234, hotel_id: 1, }, mockUtilSvc, scope, ctrl ; beforeEach(module('conciergeApp', function($provide) { $provide.value('CurrentUser', mockData); })); beforeEach(inject(function(CurrentUser, $rootScope, $controller) { mockUtilSvc = CurrentUser; scope = $rootScope.$new(); ctrl = $controller('ReplyCtrl', { $scope: scope }); })); it('should return value from mock dependency', function() { expect(mockUtilSvc.id).toEqual(mockData.id); }); });
Demo
Note. There are other ways to mock and introduce a layout. For example, in this case you really do not need to use $provide.value . Instead, you can create your mock object and pass it during the creation of the controller instance, as you have full control over this to pass the dependency, as shown below:
ctrl = $controller('ReplyCtrl', { $scope: scope, CurrentUser: mockUtilSvc });
In the case of more complex maintenance using methods, you can create a mock like this.
mockSvc = jasmin.createSpyObj('MyService', ['method1', 'method2']); mockSvc.method1.and.returnValue($q.when(mockData));