javascript - Unit Test a AngularJS Directive's Controller (Karma, Chai, Mocha) -


having trouble reaching directive's scope unit test. getting generic compile errors when trying run unit test.

my app compiles (gulp) , runs fine, , can unit test non-directive's fine well. not sure how test's work directive, trying other people's solutions educated guesses.

main page's html , js

<div company-modal="companyoptions" show-close="false"></div> 

.

(function() {     'use strict';      angular         .module('app')         .controller('companymodalctrl', ['$scope', selectionpage]);          function selectionpage($scope) {             $scope.companyoptions = {};         } })(); 

here first portion of directive (it big including important-top part.

(function() {   'use strict';    angular     .module('app')     .directive('companymodal',       companymodal     );    function companymodal() {     return {       restrict: 'a',       replace: false,       transclude: true,       templateurl: '/src/login/company.html',       scope: {         options: '=companymodal',         showclose: '='       },       bindtocontroller: true,       controller: companyselection,       controlleras: 'csmvm'     };   }    companyselection.$inject = ['$state'];    function companyselection($state) {     var csmvm = this;     var url;     csmvm.emaillist = []; 

here attempt @ unit test

'use strict';  describe('company', function () {    var scope;   var controller;   var elem;    beforeeach(module('app'));   beforeeach(inject(function ($controller, $rootscope, $compile) {     scope = $rootscope.$new();      elem = angular.element('<div company-modal="companyoptions" show-close="false"></div>');     $compile(elem)($rootscope);     /*        controller = elem.controller('companymodal csmvm');       controller = $controller(controller,        {         $scope : scope       });     */     controller = $controller(elem.controller('companymodal csmvm'), {       $scope: scope     });      scope.csmvm.emaillist.email = "bob@gmail.com";    }));    describe('invite', function () {     it('should array emaillist', function () {       expect(scope.csmvm.emaillist).to.be.an('array');     });    });  }); 

my problem (and sorry being undescriptive) keep getting run-time errors test. such as:

failed tests: company "before each" hook: workfn 18) object { "message": "'undefined' not object (evaluating '(isarray(type) : type).prototype)".

and again, app compiles (gulp) , runs fine, , can unit test non-directive's fine well

you need run expectations towards isolated scope of directive.

right now, scope referring scope directive was compiled.

but directive creates new isolate scope, , not running expectations towards it.

two ways of doing it:

function isolatescope (el) {   return el.isolatescope(); }  /** or **/  var el, scope, isolatescope;   beforeeach(inject(function ($compile, $rootscope) {   scope = $rootscope.$new();   el = $compile('<directive></directive>')(scope);   scope.$digest();   isolatescope = el.isolatescope(); }); 

with of said, remove controller out of directive spec suite , test in isolation. makes far cleaner / modular unit tests. try break them best can.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -