dimanche 30 août 2015

Test if function has been called inside $scope.$watch

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.

Aucun commentaire:

Enregistrer un commentaire