与AngularJS +茉莉单元测试指令 [英] Unit testing directive with AngularJS + Jasmine

查看:145
本文介绍了与AngularJS +茉莉单元测试指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的单元测试茉莉花,所以二希望这是有道理和足够正确的得到答案,我想测试一个angularJS指令

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

这是我的plunker: http://jsfiddle.net/ksqhmkqm/13

here is my plunker : http://jsfiddle.net/ksqhmkqm/13

在我的情况下,我无法获得输入(ID =Montid)的茉莉值

in my case i am unable to get the input (id="Montid") value in jasmine

这是我的angualr code

here is my angualr code

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

      });
    }
  };
});

这是我的茉莉花code

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

  });
});

我想读Montid值进行比较

i want to read Montid value to compare

感谢您,
切塔尼亚

Thank you, Chaitanya

推荐答案

您可以用属性的 ATTR 的功能

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

这篇关于与AngularJS +茉莉单元测试指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆