If I understand you correctly, you want to test a service that depends on another service, and make fun of the dependency for each test. If yes, let's say that we have a car that has a dependency on engine :
var app = angular.module('plunker', []) .factory('car', function(engine) { return { drive : function() { return 'Driving: ' + engine.speed(); } } }) .value('engine', { speed : function() { return 'fast'; } });
Then you want to test the car and make fun of the engine. There are two ways to do this: either by defining a new module in which we could override the engine:
describe('Testing a car', function() { var testEngine; beforeEach(function(){ testEngine = {}; angular.module('test', ['plunker']).value('engine', testEngine); module('test'); }); it('should drive slow with a slow engine', inject(function(car) { testEngine.speed = function() { return 'slow'; }; expect(car.drive()).toEqual('Driving: slow'); })); });
The desktop is here: http://plnkr.co/edit/ueXIzk?p=preview
A slightly simpler alternative, relaying the dynamic nature of JavaScript:
describe('Testing a car', function() { var testEngine; beforeEach(module('plunker')); beforeEach(inject(function(engine){ testEngine = engine; })); it('should drive slow with a slow engine', inject(function(car) { testEngine.speed = function() { return 'slow'; }; expect(car.drive()).toEqual('Driving: slow'); })); });
http://plnkr.co/edit/tlHnsJ?p=preview
Another alternative is to use Jasmine spying:
describe('Testing a car', function() { var testEngine; beforeEach(module('plunker')); beforeEach(inject(function(engine){ testEngine = engine; })); it('should drive slow with a slow engine', inject(function(car) { spyOn(testEngine, 'speed').andReturn('slow'); expect(car.drive()).toEqual('Driving: slow'); })); });
http://plnkr.co/edit/K4jczI?p=preview