Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – 如何单元测试指令中的隔离范围大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图单元测试一个简单的指令,但范围中的变量总是未定义的.

指令Src代码

.directive('ratingButton',['$rootScope',function($rootScopE) {
      return {
          reStrict: "E",replace: true,template: '<button type="button" class="btn btn-circle" ng-class="getratingClass()"></button>',scope: {
              buttonrating: "="
          },link: function(scope,elem,attr) {
              scope.getratingClass = function() {
                  if (!scope.buttonrating)
                      return '';
                  else if (scope.buttonrating.toUpperCase() === 'GREEN')
                      return 'btn-success';
                  else if (scope.buttonrating.toUpperCase() === 'YELLOW')
                      return 'btn-warning warning-text';
                  else if (scope.buttonrating.toUpperCase() === 'RED')
                      return 'btn-danger';
                  else if (scope.buttonrating.toUpperCase() === 'BLUE')
                      return 'btn-info';
              }
          }
      };
  }])

测试:

describe('Form Directive: ratingButton',function() {

    // load the controller's module
    beforeEach(module('dashboardApp'));

    var scope,element;

    // Initialize the controller and a mock scope
    beforeEach(inject(function($compile,$rootScopE) {
        scope = $rootScope.$new();

        //set our view html.
        element = angular.element('<rating-button button-rating="green"></rating-button>');
        $compile(element)(scopE);
        scope.$digest();
    }));

    it('should return appropriate class based on rating',function() {
        //console.log(element.isolateScope());
        expect(element.isolateScope().buttonrating).toBe('green');
        expect(element.isolateScope().getratingClass()).toBe('btn-success');

    });

});

我在另一个指令单元测试中使用了类似的代码,通过元素属性传递值,并且按预期工作.对于这个测试buttonrating总是不确定不知道从哪里去(我是相当新的茉莉/羯磨)

任何帮助将是伟大的!

当您在启动测试时编译指令元素时,而不是设置字符串绿色将设置为范围界限.否则,它将在绑定的范围上查找名称为green的scope属性的值,当然在您的情况下也没有定义.

即scope.buttonrating =’green’;

angular.element(‘< rating-button button-rating =“buttonrating”>< / rating-button>‘)

尝试:

// Initialize the controller and a mock scope
    beforeEach(inject(function($compile,$rootScopE) {
        scope = $rootScope.$new();
        scope.buttonrating = 'green'; //<-- Here
        //set our view html.
        element = angular.element('<rating-button button-rating="buttonrating"></rating-button>');
        $compile(element)(scopE);
        scope.$digest();
    }));

    it('should return appropriate class based on rating',function() {
        expect(element.isolateScope().buttonrating).toBe('green');
        expect(element.isolateScope().getratingClass()).toBe('btn-success');

    });

Plnkr

大佬总结

以上是大佬教程为你收集整理的angularjs – 如何单元测试指令中的隔离范围全部内容,希望文章能够帮你解决angularjs – 如何单元测试指令中的隔离范围所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。