2015-09-16 9 views
9

Sto provando a eseguire il mio test dell'unità jasmine per un servizio. Ho preso in giro la posizione $ ma ottenendo un errore:Come risolvere TypeError: spyOn andReturn non è una funzione?

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' 
     } 

    } 

    }; 
}); 

Il mio test è simile al seguente:

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'); 
     }); 
    }); 
}); 

Questo è l'errore che sto ottenendo: TypeError: spyOn (...). e il ritorno non è una funzione?

risposta

12

jasmine 2.0 ha modificato parte della sintassi della spia. jasmine 2.0 docs

Utilizzare:

spyOn($location, 'absUrl').and.returnValue('localhost');