javascript - Karma/Jasmine testing custom directive controller -
i'm trying test angularjs custom directive karma + jasmine. found way checking many references around web. solution doesn't appear correct way. let's first check example, test.js:
angular.module("app", []) .directive("test", function() { return { restrict: 'e', scope: { defined: '=' }, templatefile: "test.html", controller: function($scope) { $scope.isdefined = function() { return $scope.defined; }; } }; }); describe("test directive", function() { var elm, scope; beforeeach(module("app")); beforeeach(module("test.html")); beforeeach(inject(function($rootscope, $compile, $injector) { elm = angular.element("<test defined='defined'></test>"); scope = $rootscope; scope.defined = false; $compile(elm)(scope); scope.$digest(); })); it("should not defined", function() { expect(elm.scope().$$childtail.isdefined()).tobe(false); }); });
now directive template file test.html:
<button data-ng-click='defined = true'></button>
and karma.conf.js:
module.exports = function(config) { config.set({ basepath: '', frameworks: ['jasmine'], files: [ 'angular.min.js', 'angular-mocks.js', 'test.js', 'test.html' ], exclude: [], preprocessors: { "test.html": ['ng-html2js'] }, reporters: ['progress'], port: 9876, colors: true, loglevel: config.log_info, autowatch: true, browsers: ['firefox'], singlerun: true }); };
i running tests following command line:
karma start karma.conf.js
what appears strange me scope function defined in controller can accessed $$childtail
attribute. if try call directly element's scope got undefined value elm.scope().isdefined()
. has better solution this?
thanks!
because directive uses isolated scope, instead of
elm.scope()
you should able use
elm.isolatescope()
the function isolatescope
explained (briefly) @ http://docs.angularjs.org/api/ng/function/angular.element
Comments
Post a Comment