简体   繁体   English

AngularJS + Jasmine的单元测试指令

[英]Unit testing directive with AngularJS + Jasmine

I am a new to unit-testing with jasmine, so iI hope this makes sense and is correct enough to get an answer , I am trying to test an angularJS directive 我是使用Jasmine进行单元测试的新手,所以我希望这是有道理的,并且足够正确以获得答案,我正在尝试测试angularJS指令

here is my plunker : http://jsfiddle.net/ksqhmkqm/13 这是我的朋克: http : //jsfiddle.net/ksqhmkqm/13

in my case i am unable to get the input (id="Montid") value in jasmine 以我为例,我无法在茉莉花中获取输入(id =“ Montid”)值

here is my angualr code 这是我的angualr代码

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"));
          }
        }

      });
    }
  };
});

here is my jasmine code 这是我的茉莉花代码

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();

  });
});

i want to read Montid value to compare 我想阅读Montid值进行比较

Thank you, Chaitanya 谢谢你,柴坦亚

You can get the attribute with the attr function 您可以使用attr函数获取属性

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM