Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve TypeError: spyOn andReturn is not a function?

I am trying to run my jasmine unit test for a service. I have mocked out the $location but getting an error:

app.factory('testService', function($location) {
  return {
    config: function() {
      var host = $location.absUrl();

      var result = '';

      if (host.indexOf('localhost') >= 0) {
        return 'localhost'
      }

      if (host.indexOf('myserver') >= 0) {
        return 'myserver'
      }

    }

  };
});

My test looks like this:

describe('testing service', function () {
    var configService, $rootScope, $location;

    beforeEach(module('myApp'));
    beforeEach(inject(function (_$location_) {
        //$rootScope = _$rootScope_;
        $location = _$location_;
        //configService=_configService_;
        spyOn($location, 'absUrl').andReturn('localhost');
    }));

    it('should return something  ', function () {
        inject(function (configService) {
            //expect($location.absUrl).toHaveBeenCalled();
            //expect($rootScope.absUrl()).toBe('localhost');

            var result= configService.config($location);
            expect(result).toBe('localhost');
        });
    });
});

This is the error I am getting: TypeError: spyOn(...).andReturn is not a function?

like image 1000
Pindakaas Avatar asked Sep 16 '15 03:09

Pindakaas


Video Answer


1 Answers

jasmine 2.0 changed some of the spy syntax. jasmine 2.0 docs

Please use:

spyOn($location, 'absUrl').and.returnValue('localhost');
like image 106
Koen Avatar answered Oct 02 '22 14:10

Koen