我正在尝试为服务运行我的茉莉花单元测试。我已经嘲笑$ location但收到错误:
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'
}
}
};
});
我的测试看起来像这样:
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');
});
});
});
这是我得到的错误: TypeError:spyOn(...)。andReturn不是函数?
答案 0 :(得分:18)
jasmine 2.0改变了一些间谍语法。 jasmine 2.0 docs
请使用:
spyOn($location, 'absUrl').and.returnValue('localhost');