jeudi 9 juillet 2015

Why does this unit test fail? (Promises with Jasmine/AngularJS)

Unit test for a directive, where escope is the scope isolated

I have also tried pulling in $rootScope with $rootScope.$apply() and $rootScope.$digest() init_data() is not being called from the unit test though :(((. The modal is an angularStrap modal.

it('should open the dialog', function(){
    spyOn(escope.mainModal.$promise, "then").and.callFake(function() {
        var deferred = $q.defer();

         deferred.resolve(true);

        return deferred.promise;
    });

    spyOn(escope, 'init_data');

    escope.openDialog();
    escope.$apply();
    expect(escope.mainModal.$promise.then).toHaveBeenCalled();
    expect(escope.init_data).toHaveBeenCalled();
});

And in the directive..

$scope.openDialog = function() {

    $scope.mainModal.$promise.then(function(f) {
        debugger
        $scope.init_data();

    });

}

The first spy passes, but the init_data spy fails. For the life of me I can't figure this out.

Aucun commentaire:

Enregistrer un commentaire