The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Unit Testing Directive Scope

Unit Testing Directive Scope

1:57
Unit test your AngularJS directives that make changes to $scope as well as their own isolated scopes.
Watch this lesson now
Avatar
egghead.io

Unit test your AngularJS directives that make changes to $scope as well as their own isolated scopes.

Avatar
Martin

how do you get browserTrigger to work. When I add angular-scenario to my karma config all the tests stop running (at all).

New to this, so what am I missing?

New pro subscriber - so it has been a blast catching up with your material. Thanks.

In reply to egghead.io
Avatar
Ang

Martin, I had the same issue when I included angular-scenario.js in the 'files' section in karma.conf.js. Instead of adding angular-scenario.js to files, using the package https://npmjs.org/package/karma-ng-scenario fixed the problem for me. In short:
npm install karma-ng-scenario, then add 'ng-scenario' to the 'frameworks' section in karma.conf.js.

In reply to Martin
Avatar
Ang

As of Angular 1.2, to retrieve the isolated scope, one needs to use element.isolateScope() instead of element.scope()
http://code.angularjs.org/1.2.0/docs/api/angular.element

In reply to egghead.io
Avatar
Martin

Makes sense. Thanks for clearing that up. Much appreciated.

In reply to Ang
Avatar
marcek
ReferenceError: browserTrigger is not defined

I'm testing using version 1.3.4 of angularJS and what works for me is

element.triggerHandler 'click'

according to: https://docs.angularjs.org/api/ng/function/angular.element

In reply to egghead.io
Avatar
Marian

I've used isolateScope() to work:
expect(element.isolateScope().clicked).toBeTruthy();

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?