Use the real thing for the object / method under test,
and use spyOn to fake rest.
Make a New Unit Test File
(function() { 'use strict'; describe('controllers', function(){ var AgendaAutoScroller; beforeEach(module('agendaIpad')) beforeEach(inject(function(_AgendaAutoScroller_) { AgendaAutoScroller = _AgendaAutoScroller_; }); }) }); })();
Using spyOn To Make Fake Implementations for Methods on the Object Under Test
(function() { 'use strict'; describe('AgendaAutoScroller', function(){ var AgendaAutoScroller; beforeEach(module('agendaIpad')) beforeEach(inject(function(_AgendaAutoScroller_) { AgendaAutoScroller = _AgendaAutoScroller_; spyOn(AgendaAutoScroller,['getContainerHeight']).and.callFake(function () { return 500; }); }); }) it('should calculate 800 when I enter 400', function () { expect(AgendaAutoScroller.calculateScroll(400)).toEqual(800); }); }); })();
Using spyOn for Methods of Dependency Objects
(function() { 'use strict'; describe('AgendaAutoScroller', function(){ var AgendaAutoScroller; var HeightTracker; beforeEach(module('agendaIpad')) beforeEach(inject(function(_AgendaAutoScroller_), _HeightTracker_ { AgendaAutoScroller = _AgendaAutoScroller_; HeightTracker = _HeightTracker_; spyOn(HeightTracker,['getContainerHeight']).and.callFake(function () { return 500; }); }); }) it('should calculate 800 when I enter 400', function () { expect(AgendaAutoScroller.calculateScroll(400)).toEqual(800); }); }); })();
Bonus Tip: "Spy on" Variables
(function() { 'use strict'; describe('AgendaAutoScroller', function(){ var AgendaAutoScroller; var HeightTracker; beforeEach(module('agendaIpad')) beforeEach(inject(function(_AgendaAutoScroller_), _HeightTracker_ { AgendaAutoScroller = _AgendaAutoScroller_; HeightTracker = _HeightTracker_; HeightTracker.initialValue = true; }); }) it('should calculate 800 when I enter 400', function () { expect(AgendaAutoScroller.calculateScroll(400)).toEqual(800); }); }); })();