title: AngularJS unit testing directive with ngModel
body: <p>I have made a directive which uses ngModel:</p>
  
  <pre><code>.directive('datetimepicker', function () {
      return {
          restrict: 'E',
          require: ['datetimepicker', '?^ngModel'],
          controller: 'DateTimePickerController',
          replace: true,
  
          templateUrl: ...,
  
          scope: {
              model: '=ngModel'
          },
  
          link: function (scope, element, attributes, controllers) {
              var pickerController = controllers[0];
              var modelController = controllers[1];
  
              if (modelController) {
                  pickerController.init(modelController);
              }
          }
      }
  });
  </code></pre>
  
  <p>But when testing...</p>
  
  <pre><code>var scope, element;
  
  beforeEach(module('appDateTimePicker'));
  beforeEach(module('templates'));
  
  beforeEach(inject(function ($compile, $rootScope) {
      compile = $compile;
      scope = $rootScope;
  
      scope.model = new Date();
  
      element = compile(angular.element('&lt;datetimepicker ng-model="model"&gt;&lt;/datetimepicker&gt;'))(scope);
  
      scope.$digest();
  }));
  </code></pre>
  
  <p>I can't anyhow set value to ng-model.</p>
  
  <p>Fo example, here scope.model is a date, so scope.year and scope.month should be date and year of that model, but it is undefined.</p>
  
  <p>As seen in the directive's code, I'm using this.init on the controller to initialise all the process.</p>
  
  <p>What am I missing?</p>
  
  <h2>EDIT</h2>
  
  <p>Example of test:</p>
  
  <pre><code>it('should test', function () {
      expect(scope.model).toBe(undefined);
      expect(scope.year).toBe(undefined);
  });
  </code></pre>
  
  <h2>EDIT</h2>
  
  <p>This helped to solve the problem: <a href="http://jsfiddle.net/pTv49/3/" rel="nofollow">http://jsfiddle.net/pTv49/3/</a></p>