jeudi 21 mai 2015

angular directive data binding not enforced in unit test

I'm new to Angular and am trying to unit test a directive. The directive has an isolated scope with, what I understand to be, a mandatory 2 way binding as denoted by '=' as opposed to '=?', which I believe to be optional.

Why can I compile the HTML and trigger a digest without error, even when the mandatory attribute is missing?

Directive

angular.module('myModule').directive('doStuff', function() {
  'use strict';
  return {
    'restrict': 'E',
    'replace': true,
    'scope': { foo : '=' },
    'controller': 'MyCtrl',
    'controllerAs': 'ctrl',
    'templateUrl': 'foo.html'
  };
});

Test

describe('doStuff', function () {
  var scope, compile, $rootScope;

  beforeEach(function () {
    module('myModule');

    inject(function (_$rootScope_, _$compile_) {
      $rootScope = _$rootScope_;
      scope = $rootScope.$new();
      compile = _$compile_;
    });
  });

  it('should interpolate data', function () {
    // attribute missing, but no error
    var element = compile('<do-stuff></do-stuff>')(scope); 
    $rootScope.$digest();
    // test stuff here
  });
});

Aucun commentaire:

Enregistrer un commentaire