我最近開始學習如何測試與業力/茉莉花的角度。angularjs測試驗證事件被捕獲
這裏是場景: 我有一個指令,監聽一個事件,當它被調用時,它調用指令作用域上的函數。
這是我的測試。問題是該函數從未被調用過。下面的代碼不起作用,因爲間諜無法找到偵聽器所調用的函數。指令函數更新listItems值。不是很TDD,但目前它只能在現實中,而不是在測試:/
指令
(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);
}));
我很困難,接下來該做什麼。這是根據lght的回答進行的更新。如何解決,以便事件在測試中被捕獲?間諜似乎不是被叫做。奇怪,因爲我正在播出這個活動!
,而不是試圖去檢查,如果該事件被逮住,我在做什麼的檢查,如果範圍被嘲諷偵聽特定事件'$ on' – GeoffreyB 2014-10-07 15:21:40
好吧,你有一個例子嗎? – JohanLarsson 2014-10-07 15:25:54
由於您在'$ on'上添加了間諜,您不應該再播放您的活動。否則,你應該監視你的指令的作用域,而不是'$ rootScope' – GeoffreyB 2014-10-08 08:19:16