在单元测试中访问AngularJS指令的范围

时间:2018-04-05 15:28:30

标签: javascript angularjs unit-testing angular-directive

我正在为AngularJS指令编写单元测试,它在作用域上进行了一些函数调用,并且在这些函数中,它在作用域上创建了新对象。

所以我已经创建了我的测试:

fdescribe('Activity Directive Tests - ', function() {
  beforeEach(module('starter.services'));

  window.RESOURCE_ROOT = 'foo/';

  var $scope, $compile, element, $httpBackend;
  beforeEach(inject(function($rootScope, _$compile_, $injector) {
    $scope = $rootScope.$new();
    $compile = _$compile_;

    $httpBackend = $injector.get('$httpBackend');
    $httpBackend.expectGET('foo/shared/activity/activity-modal.html').respond('200','');

    element = $compile('<activity-button></activity-button>')($scope);



    $scope.$digest();
  }));


  describe('Tests of objects created when page opened - ', function() {
    it('should create a empty task obj', function() {
      element.scope().openActivity(); // call function of directive
      expect(element.scope().task).toBeDefined();
    })
  })

})

但是我继续保持范围未定义。我们所有的指令只使用链接函数而不是控制器,所以我认为访问范围并不容易。我试过访问隔离范围,但这也是未定义的。

这是指令的设置方式:

(function() {
  'use strict';

  angular
    .module('starter.services')

    .directive('activityButton', function(
      $window,
      SobjectService,
      $stateParams,
      $ionicModal,
      $ionicLoading,
      $location,
      LOG_TIME_CONSTANTS,
      $filter
    ) {
      return {
        restrict: 'E',
        scope: {},
        link: function(scope) {
          scope.activityModel = {};
          scope.activityModel.ActivityType__c;

          scope.activityTaskModel = {};
          scope.activityTaskModel.TaskType__c;

          scope.activityEventModel = {};
          scope.activityEventModel.EventType__c;
          scope.ActivityType = 'Task';

          scope.isValid = {};

          $ionicModal
            .fromTemplateUrl(
              $window.RESOURCE_ROOT + 'shared/activity/activity-modal.html',
              {
                scope: scope,
                animation: 'slide-in-up'
              }
            )
            .then(function(modal) {
              scope.modal = modal;
            });

          scope.openActivity = function ()  {
            scope.activity = {};
            scope.task = {};

            if ($stateParams.Id) {
              var objectName = $location.path().split('/')[1];
              console.log(objectName);
              if (
                objectName == LOG_TIME_CONSTANTS.PAGE_LEAD ||
                objectName == LOG_TIME_CONSTANTS.PAGE_CONTACT
              ) {
                getLeadDetails($stateParams.Id);
              } else {
                //for Opportunity,Account
                getRecordDetails($stateParams.Id);
              }
            }

            scope.modal.show();
          };


          scope.saveTask = function() {
            var taskToSave = buildTaskRecord();
            isTaskValid(taskToSave);

            if (scope.isValid.valid) {
              SobjectService.insert('Task__ap', taskToSave)
                .then(function(res) {
                  showToast('Task successfully saved.');
                  scope.close(scope.modal);
                })
                .catch(function(e) {
                  console.error(e);
                  showToast('Could not save Task.');
                });
            } else {
              showToast(
                'Could not save the Task, some fields are still not complete'
              );
            }
          };


        templateUrl:
          $window.RESOURCE_ROOT + 'shared/activity/activitybutton.html'
      };
    });
})();

0 个答案:

没有答案