You haven't defined 'ind' anywhere in that JavaScript file. The ng-click should work as $index is defined in the ng-repeat. But your test has declaration of 'ind'.
Jasmine and Angularjs - Why $index is undefined?
-
19-06-2023 - |
Domanda
The script works, but not if I test it.
Jasmine:
describe("myApp", function(){
beforeEach(module("mymod"));
describe("maincontroller", function(){
var scope;
var ctrl;
var compile;
var html;
beforeEach(inject(function($rootScope, $controller, $compile){
scope = $rootScope.$new();
ctrl = $controller("maincontroller", {$scope:scope});
compile = $compile;
html = angular.element('<ul id="listONE"><li class="{{person.isSel}}" ng-repeat="person in people" ng-click="selPersonToChange($index)">{{person.name +" - "+ person.city}}</li></ul>');
compile(html)(scope);
scope.$digest();
}));
it("Is selPersonToChange() working?", function(){
expect(scope.selPersonToChange(ind)).toEqual(true);
});
});
Soluzione
Altri suggerimenti
You aren't giving selPersonToChange
any index to change, it should be
it("Is selPersonToChange() working?", function(){
var ind = 0;
scope.selPersonToChange(ind);
// scope.$digest(); // only needed if it needs to reflect on your DOM
expect(scope.people[ind].isSel).toEqual(true);
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow