angularjs测试验证事件是否被捕获
我最近开始学习如何用karma/jasmine测试角度 以下是场景: 我有一个侦听事件的指令,当调用它时,它会调用指令范围内的函数 这是我的测验。问题是该函数从未被调用。下面的代码不起作用,因为spy找不到侦听器调用的函数。指令函数更新listItems值。不是很TDD,但目前它仅在现实中有效,在测试中无效:/ 指示 测试代码,在每个angularjs测试验证事件是否被捕获,angularjs,unit-testing,tdd,karma-runner,Angularjs,Unit Testing,Tdd,Karma Runner,我最近开始学习如何用karma/jasmine测试角度 以下是场景: 我有一个侦听事件的指令,当调用它时,它会调用指令范围内的函数 这是我的测验。问题是该函数从未被调用。下面的代码不起作用,因为spy找不到侦听器调用的函数。指令函数更新listItems值。不是很TDD,但目前它仅在现实中有效,在测试中无效:/ 指示 测试代码,在每个 我在苦苦思索下一步该怎么办。这是根据lght的答案进行的更新。如何修复,以便在测试中捕获事件?“间谍”似乎没有被叫来。奇怪,因为我在广播这件事 我所做的不是检查事
我在苦苦思索下一步该怎么办。这是根据lght的答案进行的更新。如何修复,以便在测试中捕获事件?“间谍”似乎没有被叫来。奇怪,因为我在广播这件事 我所做的不是检查事件是否被捕获,而是通过模拟$on检查作用域是否正在侦听特定事件 这一切都是关于spyOn的不要用spyOn,你所需要做的就是广播这个事件,其余的应该自动通过 它“应该调用事件”,=>{ 间谍镜,“刷新列表”; 范围。$广播“刷新列表项”; expectscope.refreshList.To已被调用;
};我所做的不是检查事件是否被捕获,而是通过模拟$onOk检查作用域是否正在侦听特定事件,您有没有示例?既然您在$on上添加了一个间谍,您就不应该再广播您的事件了。否则,您应该监视指令的作用域,而不是$rootScopeok。我现在已将作用域更改为$rootScope.new。我从父范围进行广播,在本地范围进行监视。scope.$parent.$broadcastrefreshListItems,scope.affectedValues;spyOnscope,$on.andCallFakefunction事件,影响值{};适用范围:$适用``expectscope.$on.to已装入;我仍然得到一个错误,说预期间谍$on已被调用我尝试了类似上述的东西,但没有运气。。。请看我修改过的问题。看来这个间谍没有被叫来
(function() {
angular.module('app').directive('listComponent', function(utilities) {
return {
restrict: 'E', //element
templateUrl: 'views/listComponent.html',
scope: {
listItems: '='
},
replace: true,
link: function (scope, element, attributes) {
scope.title = attributes["title"];
scope.listItemsGrouped = [];
var primary = attributes["primaryField"];
var itemsGrouped = _.groupBy( scope.listItems, function (obj) {
return obj[primary];
});
scope.listItems = [];
angular.forEach(itemsGrouped[3016],function(item){
scope.listItems.push({dimValueParent: item.dimValueParent,dimValueChild: item.dimValueChild, value: item.value});
});
scope.$on('refreshListItems',
function(evt,affectedValues) {
scope.refreshList(affectedValues);
});
},
controller: function($scope){
$scope.refreshList = function(vals) {
//handle values affected by the update
angular.forEach(vals, function(affectedValue) {
var indexParent = utilities.findIndexOf($scope.listItems,"dimValueParent",affectedValue.Dimensions[0].Value);
var indexChild = utilities.findIndexOf($scope.listItems,"dimValueChild",affectedValue.Dimensions[1].Value);
if (indexParent > -1 && indexChild > -1) {
$scope.listItems[indexChild].value = affectedValue.CalculatedValue;
}
});
}
}
}
});
}());
beforeEach(inject(function ($compile, $rootScope) {
scope = $rootScope;
localScope = $rootScope.$new();
ele = angular.element(
'<list-component title="Testing generic list" list-items="list" ></list-component>'
);
mockupDataFactory = konstruktMockupData.getInstance();
//these variables are needed.
scope.data = mockupDataFactory.pivotedData;
scope.listItems = [
{dimValueParent: "3016",dimValueChild: "100", value:101},
{dimValueParent: "3016",dimValueChild: "110", value:102},
{dimValueParent: "3016",dimValueChild: "120", value:103}];
scope.affectedValues = [
{CalculatedValue: 1000,Dimensions:[{ Key: "1", Value: "3016" }, { Key: "4", Value: "100" }]},
{CalculatedValue: 1100,Dimensions: [{ Key: "1", Value: "3016" }, { Key: "4", Value: "110" }]},
{CalculatedValue: 1200,Dimensions: [{ Key: "1", Value: "3016" }, { Key: "4", Value: "120" }]}];
scope.$apply();
}));
it('Should handle an update listItems event', inject(function (){
var affectedAccountsAfterUpdateExpcted = [
{dimValueParent: "3016",dimValueChild: "100", value:1000},
{dimValueParent: "3016",dimValueChild: "110", value:1100},
{dimValueParent: "3016",dimValueChild: "120", value:1200}];
//spyOn(localScope, "refreshList");
scope.$broadcast("refreshListItems", scope.affectedValues);
spyOn(scope, "$on").andCallFake(function (event, affectedValues) {
expect(affectedValues).toBe(scope.affectedValues);
expect(event).toEqual("refreshListItems");
});
expect(scope.$on).toHaveBeenCalled();
scope.$apply();
expect(scope.listItems).toBeDefined();
//expect(scope.listItems).toEqual(affectedAccountsAfterUpdateExpcted);
}));
describe("Testing the initialization", function () {
beforeEach(function () {
controller = $controller("connectionBannerCtrl", {
$rootScope: $rootScope,
$scope: $scope,
ConnectionBanner: ConnectionBanner
});
});
it("should subscribe to the 'ConnectionBannerChanged' event", function () {
spyOn($rootScope, "$on").andCallFake(function (event, callback) {
expect(callback).toBe($scope.setup);
expect(event).toEqual("ConnectionBannerChanged");
});
controller = $controller("connectionBannerCtrl", {
$rootScope: $rootScope,
$scope: $scope,
ConnectionBanner: ConnectionBanner
});
expect($rootScope.$on).toHaveBeenCalled();
})
});