jeudi 1 octobre 2015

Angularjs Unit Tests not modeling data from SELECT node

I'm new to angularjs and trying to Unit Test my directive. I actually tested it in a fully-working browser session first because it seemed easier, but I wanted to get into unit testing because it's best practice.

In my browser session, my directive functions exactly as I expect it to. It creates a <select> tag that uses ng-model to bind to a property on the $rootScope.

However, when I test with Karma using Jasmine syntax (as the Angular docs recommend), I'm not able to get the model update on the $rootScope when I change the value of the <select> element.

Here's my Jasmine code. It's a snippet, but I assure you that the variables not explicitly defined in the below code are correctly defined using $inject(). I've tested them out to make sure they weren't screwing up:

var el = $compile("<my-directive my-model='myModel'></my-directive>")($scope);
//the 'my-model' value, 'myModel', gets assigned during the link phase to:
//$scope.$root.fields[myModel]
var $el = angular.element(el);
$el.find('select').val('someVal');
$rootScope.$digest();
expect($scope.$root.fields[myModel]).toEqual("someVal");

The error that I get is:

Expected '' to equal 'someVal'

What am I doing wrong? Am I misunderstanding the $digest() phase? Or how ng-model works?

Aucun commentaire:

Enregistrer un commentaire