mardi 1 mars 2016

Unit Test Directive Method does not exist

I am trying to test a directive:

define(['require'], function (require){
'use strict';

var myDirective = function ($rootScope, MyService) {

    return {
        restrict: 'E',
        templateUrl: 'app/banner/banner.html',
        scope: {},
        controller: [ '$scope', '$element', function ($scope, element) {                

            $scope.sendEmail = function(data) {
                $scope.showConfirmation = false;
                $rootScope.$broadcast('process-email', data);
            };
        }]
    };
};

 return ['$rootScope', 'MyService', myDirective];
});

banner.html

<ul>
    <li><a href="javascript:;" ng-click="sendEmail('process')" class="email-item">Send mail</a></li>
    <li><a href="javascript:;" ng-click="sendEmail('clarify')" class="email-item">Clarify mail</a></li>
</ul>

Test:

define(['require', 'angular-mocks'], function (require) {
    'use strict';

    var angular = require('angular');

    describe('MyDirective Spec ->', function () {

        var scope, $compile, element, $httpBackend;

        beforeEach(angular.mock.module('MyApp'));

        beforeEach(inject(function (_$rootScope_, _$httpBackend_) {
            scope = _$rootScope_.$new();
            $compile = _$compile_;
            $httpBackend = _$httpBackend_;
            var html = '<div my-directive></div>';
            element = $compile(angular.element(html))(scope);
            scope.$digest();
        }));

        it('should test sendEmail', function () {
            spyOn(scope, 'sendEmail').and.callThrough();
            element.find('.email-item').click();
            expect(scope.sendEmail).toHaveBeenCalled();
        });
    });
});

I get error:

Error: sendEmail() method does not exist

Aucun commentaire:

Enregistrer un commentaire