【问题标题】:check attribute from html - angular directive testing jasmine karma从 html 检查属性 - 角度指令测试 jasmine karma
【发布时间】:2017-05-20 14:05:47
【问题描述】:

如何检查 HTML 中存在的属性并匹配其值。这是我写的一个测试 spec.js,

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-version

'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属性并匹配属性值。

【问题讨论】:

    标签: angularjs angularjs-directive jasmine karma-runner karma-jasmine


    【解决方案1】:

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

    it('should check the angular version number', angular.mock.inject(function() {
    
           expect(tpl.attr('ng-version')).toEqual(angular.version.full);
    
       }));
    

    【讨论】:

      猜你喜欢
      • 2017-01-03
      • 1970-01-01
      • 2020-12-07
      • 2016-06-24
      • 1970-01-01
      • 2015-11-12
      • 2014-05-01
      • 2013-12-10
      • 2015-06-18
      相关资源
      最近更新 更多