angularjs - Jasmine - unit testing a directive: Getting 'undefined' is not an object -
i taking baby steps in jasmine, please bear me blatant mistakes..i writing test cases check if controller method - transformdata gets called, details below
my directive
angular.module('mymodule') .directive('mydirective', [ function () { 'use strict'; return { restrict: 'e', templateurl: '/static/quality/scripts/directives/hh-star-rating.html', scope: { vara:'@', }, controller: [ '$scope', '$controller', function ($scope, $controller) { $controller('othercontroller', {$scope: $scope}) .columns( $scope.x, $scope.y, $scope.series ); $scope.transformdata = function(data) { /// something; return data; }; } ],
my spec
describe('directive - hh-star-ratings', function() { 'use strict'; angular.module('mymodule', []) .directive('mycontainer', function() { return { restrict: 'e', priority: 100000, terminal: true, template: '<div></div>', controller: function($scope) { $scope.loadingdata = true; this.stoploading = function() { $scope.loadingdata = false; }; } } }); var result_set = { //some data-transform-req }; beforeeach(module('mymodule')); var element, scope, controller; beforeeach(inject(function($rootscope, $compile) { scope = $rootscope.$new(); element = angular.element( '<my-container> <my-directive> </my-directive> </my-container>' ); $compile(element)(scope); scope.$digest(); controller = element.controller('mydirective'); })); it('should call transformdata', function() { controller.transformdata(result_set); scope.$apply(); scope.transformdata.should.have.been.called; });
})
issue: when run test, following error
typeerror: 'undefined' not object (evaluating 'controller.transformdata')
what doing wrong? in advance time.
your define function transformdata
in scope - not in controller
controller: [ '$scope', '$controller', function ($scope, $controller) { $controller('othercontroller', {$scope: $scope}) .columns( $scope.x, $scope.y, $scope.series ); this.transformdata = function(data) // <= change (controller) { /// something; return data; }; } ],
Comments
Post a Comment