mercredi 27 avril 2016

Angular mock scope function in then

I am trying to test my controller, which creates a promise and acts upon its resolve. Unfortunately, in the then function it takes the function from a different scope than I would like it to. In my test:

it('should do sth', function () {
   $scope.foo=function(){
   return "a";
}
   SomeController.bar();

expect...
})

In my controller:

$scope.foo=function(){
return "b";}
function(){
     //all good here, "a" is returned
     var test=$scope.foo()
     return somePromiseFunction(data)
              .then(function (newData) {
                  //This function returns "b"
                  var item= $scope.foo();
    })
}

I tried adding a spy (with and without callFake and/or callThrough):

spyOn($scope, 'foo')

Aucun commentaire:

Enregistrer un commentaire