0

如何检查属性是否存在于HTML中并与其值匹配。这是我写的一个测试spec.js,从html中检查属性 - 角度指令测试jasmine karma

define(['angular', 
     'angularMocks', 
     'site-config', 
     'ng_detector', 
    ], 
    function(angular, 
     mock, 
     $app, 
     ng_detector) { 

     describe('ng-detector controller', function() { 

      beforeEach(angular.mock.module("webapp")); 

      var $compile, $rootScope, tpl, $scope, elm, templateAsHtml; 

      beforeEach(angular.mock.inject(function(_$compile_, _$rootScope_) { 

       $compile = _$compile_; 

       $rootScope = _$rootScope_; 
       // $scope = _$rootScope_.$new(); 

      })); 

      it('should initialize the ng-detector directive', inject(function() { 

       var tpl = $compile("<div ng-detector ></div>")($rootScope); 

       $rootScope.$digest(); 

       console.log(tpl) // Log: r{0: <div ng-detector="" class="ng-scope" ng-verison="1.6.4"></div>, length: 1} 

       templateAsHtml = tpl[0].outerHTML; 

       expect(templateAsHtml.attr('ng-version')).toEqual(angular.version.full); 

      })); 

     }); 
    }); 

指令。这增加了角版本属性NG版

'use strict'; 

define(['app-module'], function(ng) { 

    $app.info('ng detector initialized. {file: directives/ng-detector.js}'); 

    ng.directive('ngDetector', function() { 
     return { 
      restrict: "A", 
      link: function(scope, elm, attr) { 
       elm.attr('ng-version', angular.version.full); 
      } 
     }; 
    }); 
    return ng; 
}); 

我希望得到一个ng-version属性由指令设置和匹配的属性值。

回答

0

我想通了自己。我正在看着不同的地方。

it('should check the angular version number', angular.mock.inject(function() { 

     expect(tpl.attr('ng-version')).toEqual(angular.version.full); 

    })); 
相关问题