Angularjs 如何使用jasmine和karma正确模拟$location.url()
在我的实际js中,我尝试从url获取查询参数,如下所示:Angularjs 如何使用jasmine和karma正确模拟$location.url(),angularjs,angularjs-directive,jasmine,karma-jasmine,Angularjs,Angularjs Directive,Jasmine,Karma Jasmine,在我的实际js中,我尝试从url获取查询参数,如下所示: let string = $location.url().split('string=')[1]; 并将其等同于范围变量: $scope.string = string; loadAnotherFunction(); // called next 我写了以下内容作为测试: let $rootScope, $compile, $location; beforeEach(angular.mock.module('module.direct
let string = $location.url().split('string=')[1];
并将其等同于范围变量:
$scope.string = string;
loadAnotherFunction(); // called next
我写了以下内容作为测试:
let $rootScope, $compile, $location;
beforeEach(angular.mock.module('module.directives'
, function($provide){
let url = '/test/url';
const location = {
url: jasmine.createSpy('url').and.returnValue(url)
};
$provide.value('$location', location);
}));
beforeEach(angular.mock.inject(['$compile', '$location', '$rootScope',
function(_$compile_, _$location_ ,_$rootScope_) {
$compile = _$compile_;
$location = _$location_;
$rootScope = _$rootScope_;
}]));
<!-- another beforeEach which compiles the element & scope and digests and applies scope-->
it('replaces directive correctly', function(){
expect(element.html()).toContain('div');
});
让$rootScope、$compile、$location;
beforeach(angular.mock.module('module.directives'
,功能($提供){
让url='/test/url';
常数位置={
url:jasmine.createSpy('url')。和.returnValue(url)
};
$provide.value(“$location”,location);
}));
beforeach(angular.mock.inject(['$compile','$location','$rootScope',],
函数($compile、$location、$rootScope){
$compile=\$compile;
$location=$location;
$rootScope=\u$rootScope;
}]));
它('正确替换指令',函数(){
expect(element.html()).toContain('div');
});
但我一直在
ReferenceError:$未定义位置
这不是很好的解释,所以我不确定这里的问题是什么。我相信我正确地注入了它,它可以访问它(因为它会抛出"unknownLocationProvider在哪一行中出现错误?因为这不是angularjs错误-未声明变量。除此之外,您可以使用$Provider.value或jasmine spy-使用这两个选项看起来很奇怪。@PetrAveryanov没有说明错误msgs中的确切行,这就是为什么我感到困惑的原因。我分别尝试了这两个选项,然后又尝试了一起使用-我没有使用去测试因果报应,所以我不确定rules@PetrAveryanov错误来自karma,用于测试angularjs