mardi 28 avril 2015

unit testing promises in Angular controller with a watcher

I have a controller that 1. waits for a promise to be resolved 2. sets a watcher

both promise and watcher handlers modify a property defined on the scope of the controller.

a pseudo test would look something like this:

it('should expect correct values set on the scope', function () {
  myPromise.resolve(); // modifies someVar in promise handler

  $scope.$digest(); // for the promise handler being applied; modifies someVar in $watch handler though

  expect(someVar).toEqual(someValue);
})

the problem is $digest() in the spec forces the watcher being fired and this leads to a possible conflict as it modifies someVar's value, which is the subject of the promise test. I want to isolate the promise handler test, ideally without triggering $digest and the watcher. any ideas?

Aucun commentaire:

Enregistrer un commentaire