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?
jasmine 2.0 changed some of the spy syntax. jasmine 2.0 docs
Please use:
spyOn($location, 'absUrl').and.returnValue('localhost');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With