I have the following angular controller
function IndexCtrl($scope, $http, $cookies) { //get list of resources $http.get(wtm.apiServer + '/v1/developers/me?access_token=' + $cookies['wtmdevsid']). success(function(data, status, headers, config) { // snip }). error(function(data, status, headers, config) { // snip }); $scope.modal = function() { // snip } return; } What I am trying to do is mock the get method on the $http service. Here's my unit test code:
describe('A first test suite', function(){ it("A trivial test", function() { expect(true).toBe(true); }); }); describe('Apps', function(){ describe('IndexCtrl', function(){ var scope, ctrl, $httpBackend; var scope, http, cookies = {wtmdevsid:0}; beforeEach(inject(function($injector, $rootScope, $controller, $http) { scope = $rootScope.$new(); ctrl = new $controller('IndexCtrl', {$scope: scope, $http: $http, $cookies: cookies}); spyOn($http, 'get'); spyOn(scope, 'modal'); })); it('should create IndexCtrl', function() { var quux = scope.modal(); expect(scope.modal).toHaveBeenCalled(); expect($http.get).toHaveBeenCalled(); }); }); }); When I run this I get ReferenceError: wtm is not defined.
wtm is a global object and of course it wouldn't be defined when I run my test because the code that it is declared in is not run when I run my test. What I want to know is why the real $http.get function is being called and how do I set up a spy or a stub so that I don't actually call the real function?
(inb4 hating on globals: one of my coworkers has been tasked with factoring those out of our code :) )