Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I access an element's angularjs $ngModelController in a jasmine unit test?

I'm currently using directiveElement.data("$ngModelController") to get access to the element's $ngModelController, as in the following example.

describe("directiveElement", function () {
  it("should do something with ngModelController", inject(function($compile, $rootScope) {
    var directiveElement = $compile("<input ng-model="myNgModel" customDirective type="text"></input>")($rootScope);
    $rootScope.$digest();
    var ngModelCtrl = directiveElement.data("$ngModelController");
    ngModelCtrl.$modelValue = "12345";
    // do rest of test
  }));
});

However, I want to know if there is a better to access the $ngModelController, or if accessing the $ngModelController is a bad idea?

like image 438
JJohnston Avatar asked Apr 05 '13 14:04

JJohnston


1 Answers

You could also do directiveElement.controller('ngModel').

I certainly think there are legitimate testing reasons why you would want a handle on this, though the more common way is to get a handle on it through the form (eg. https://github.com/angular/angular.js/blob/master/test/ng/directive/formSpec.js)

like image 186
jonc Avatar answered Nov 03 '22 00:11

jonc