我正在尝试为服务运行 jasmine 单元测试。我模拟了 $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 不是函数?
最佳答案
Jasmine 2.0 改变了一些 spy 语法。查看Jasmine 2.0 docs .
请使用:
spyOn($location, 'absUrl').and.returnValue('localhost');
关于javascript - 如何解决TypeError : spyOn andReturn is not a function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32599346/