gpt4 book ai didi

unit-testing - 在 Jasmine 测试中模拟 angular.element

转载 作者:行者123 更新时间:2023-12-03 21:55:08 25 4
gpt4 key购买 nike

我在一个有调用的 Controller 中有一个函数

var someVar = angular.element(event.target).scope().field;

我试图通过这样做来模拟它
var ngElementFake = function(el) {
return {
scope: function() {
return {
toggleChildElement: true,
field: scope.field
}
}
}
}

spyOn(angular, 'element').andCallFake(ngElementFake);

但是,当我在测试中调用该函数时,我得到了响应:
TypeError: 'undefined' is not a function (evaluating 'injector.get('$rootElement').off()')
at ../angular-mocks/angular-mocks.js:1819

我究竟做错了什么?

编辑:注入(inject)
    beforeEach(function() {
inject(function($rootScope, $controller) {

scope = $rootScope;

scope.record = recordData;

scope.model = 'Hierarchy';

ctrl = $controller("fngHierarchyChildCtrl", {
$scope: scope
});
});
});

最佳答案

我可以通过在回调后手动清除 spy 来解决这个问题。

var spy;

beforeEach(function() {
spy = spyOn(angular, 'element').andCallFake(ngElementFake);
});

afterEach(function() {
spy.andCallThrough();
});

关于unit-testing - 在 Jasmine 测试中模拟 angular.element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19447666/

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