1

I'm trying to figure out how to test with karma jasmine if function was executed inside $watch with the conditions I need.

Here what is in the controller. $watch contains couple of condition.

 $scope.$watch('player', function (newVal, oldVal) {
    if (oldVal && newVal != undefined && newVal != oldVal) {
        if (newVal.email == oldVal.email && newVal.emWallet == oldVal.emWallet)
            $scope.saveSettings();
    }
}, true)

This is the part of the test

it('when player property is changed saveSettings calls', function () {

    var sspy = spyOn(scope, "saveSettings");
    expect(scope.player.email).toEqual('');
    expect(scope.player.emWallet).toEqual('');

    expect(scope.player.balance).toEqual(10.0000);

    //change player balance
    scope.player.balance = 10.0304;
    scope.$apply();

    expect(scope.player.email).toEqual('');
    expect(scope.player.emWallet).toEqual('');

    expect(sspy).toHaveBeenCalled();

});

In the test above I'm changing the property that is outside condition so player.email and player.emWallet still the same and expect the call of saveSettings() function inside, but get "sspy has never been called" error.

I would appreciate a lot if someone point me right direction.

Update: When I change actual value scope.player.balance = 10.0304; in my code, the function fires, but test does not pass successfully

rwety
  • 7
  • 1
  • 4
  • Check out [this question](http://stackoverflow.com/q/19455501/398606) and it's answer :) – Sunil D. Aug 31 '15 at 05:42
  • Thanks for suggestion but is it not my answer. First of all the question about testing, secondly this code works and function fires when I change actual value scope.player.balance = 10.0304; in my code, but not on test – rwety Aug 31 '15 at 12:29
  • Ever figured this one out? Having the exact same problem myself. Thanks. – Fernando Sep 14 '15 at 21:55

0 Answers0