expected a spy but got undefined?

19,396

scope.pickCard() is result of method. In your case it is undefined. When you are using spyOn you should write:

expect(scope.pickCard).toHaveBeenCalled();

Share:
19,396
Pindakaas
Author by

Pindakaas

Smooth as peanutbutter but hard as chocolate.

Updated on June 15, 2022

Comments

  • Pindakaas
    Pindakaas about 2 years

    Trying to get my head around jasmine spies, this is what my test looks like:

      $scope.switchTurns = function () {
        $scope.playerTurn = !$scope.playerTurn;
        console.log($scope.centrePileCards.length);
        if ($scope.playerTurn == 1) {
          $scope.pickCard();
        }
      }
    

    My unit test looks like this:

    it('should pick one card',function(){
        var controller = createController();
        spyOn(scope,'pickCard')
        scope.switchTurns();
        scope.playerTurn=1;
        expect(scope.pickCard()).toHaveBeenCalled();
    
      })
    

    I am getting this error now:

    Error : Expected a spy , but got undefined.
    

    Suggestions?