【问题标题】:Unit testing directive with AngularJS + JasmineAngularJS + Jasmine 的单元测试指令
【发布时间】:2015-10-01 05:02:33
【问题描述】:

我是 jasmine 单元测试的新手,所以我希望这是有道理的并且足够正确以获得答案,我正在尝试测试 angularJS 指令

这是我的小伙伴:http://jsfiddle.net/ksqhmkqm/13

在我的情况下,我无法获取 jasmine 中的输入 (id="Montid") 值

这是我的角度代码

app.directive("monthNext", function () {

  console.log('massif');
  return {
    restrict: 'A',
    link: function (scope, element) {
      element.on('input', function () {
        var todaysYear = new Date();
        var u = todaysYear.getFullYear() - 2;

        if (element.val().length == 4) {

          var nextElement = element.next().next().next().next().next().next().next();
          nextElement = angular.element(document.querySelectorAll('#Montid'));

          if (element.val() <= u) {

            console.log(element.children());
            //var nextElement = angular.element(document.body).find('[tab index = 6]')
            console.log(nextElement);
            //nextElement.focus();

            console.log(nextElement);
            nextElement.val("");
            nextElement[0].focus();
          } else {
            // alert(nextElement.val());             
            console.log(nextElement.val("01"));
          }
        }

      });
    }
  };
});

这是我的茉莉花代码

describe('CommonBusInfo', function () {
  var element, scope, timeout;
  beforeEach(function () {
    module('CommonBusInfo');

    inject(function ($rootScope, $compile) {
      scope = $rootScope.$new();
      element = angular.element('<form><input id="Montid" ng-model="test" value="09" type="text"/><input id="yearId" " type="text" value="2015" month-next/></form>');
      $compile(element)(scope);
      scope.$digest();
    });
  });
  it('should set Month value to 1', function () {
    var x = element.find('input');
    x.triggerHandler('input');
    scope.$digest();

  });
});

我想读取 Montid 值进行比较

谢谢你, 柴坦尼亚

【问题讨论】:

    标签: angularjs unit-testing jasmine directive


    【解决方案1】:

    可以通过attr函数获取属性

    it('should set Month value to 1', function () {
      var x = element.find('input');
      var inputId = x.attr('id');
      expect(inputId).toBe('Montid');
    });
    

    【讨论】:

    • 嗨 Koen,如果我保留 yearId 值 2015,我的 Montid 应该是 09,如果我保留 yearId 值 2015,我的 Montid 应该是''我应该如何在 jasmine 中测试。
    猜你喜欢
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 2013-08-29
    • 2014-07-01
    • 1970-01-01
    • 2013-01-23
    • 2013-08-25
    • 1970-01-01
    相关资源
    最近更新 更多