gpt4 book ai didi

javascript - 如何解决TypeError : spyOn andReturn is not a function?

转载 作者:可可西里 更新时间:2023-11-01 02:03:00 24 4
gpt4 key购买 nike

我正在尝试为服务运行 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com