Encountering an issue with the unit test
In my code, I have:
describe('test controller', function () {
var =$compile, scope, rootScope;
beforeEach(module('myApp'));
beforeEach(inject(function (_$compile_, _$rootScope_) {
$compile = _$compile_;
rootScope = _$rootScope_;
scope = _$rootScope_.$new();
}));
describe('test', function() {
beforeEach(function () {
scope.toys = ['toy1', 'toy2'];
});
it('should test directive' , function() {
var element = $compile('<button type="button" show-item>See all</button>')($rootScope);
element.triggerHandler('click');
$rootScope.$digest();
});
});
});
Here is the HTML:
<button type="button" show-item>See all</button>
The Directive:
angular.module('myApp').directive('showItem',
function() {
return {
restrict: 'A',
scope: false,
link: function(scope, elem, attrs) {
elem.bind('click', function() {
var l = scope.toys.length;
//other codes
});
}
});
When running the unit test, I am encountering an error stating '
undefined' is not an object (evaluating 'scope.toys.length')
.
I am unsure why this error is occurring, as I have specified scope.toys
inside the beforeEach function. Any assistance on this matter would be greatly appreciated. Thank you!