jeudi 25 juin 2015

Jasmine - unit testing a directive: Getting 'undefined' is not an object

I am taking baby steps in Jasmine, please bear with me for any blatant mistakes..I am writing test cases to check if a controller method - transformData gets called, the 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)
                    {
                        /// does something;

                        return data;
                    };
            }
        ],

My Spec

describe('directive - hh-star-ratings', function() {
'use strict';
angular.module('hhReport', [])
.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> </hh-datacard>'
    );

    $compile(element)(scope);

    scope.$digest();

    controller = element.controller('hhStarRating');

  }));

it('should call the transformData', function() {
    controller.transformData(result_set);
    scope.$apply();
    scope.transformData.should.have.been.called;
});

})

Issue: When I run the test, I get the following error

TypeError: 'undefined' is not an object (evaluating 'controller.transformData')

What am I doing wrong? Thanks in advance for your time.

Aucun commentaire:

Enregistrer un commentaire