jeudi 25 juin 2015

How can I test a controller that has as dependency a factory with a $http in angularjs

I am having problems trying to mock this factory ina jasmine unit test.

Let's say I have this factory:

angular.module('app').factory('myFactory', function($http) {
    var myFactory = {};
    myFactory.save = function() {
        return $http.post('myurl');
    }
    return myFactory;
});

And this controller that depends on this factory:

angular.module('app',[])
.controller('myController',function(myFactory,$scope) {
    var ctrl = this;
    ctrl.save = function(){
        myFactory.save().success(function() {
            //do something
        }).error(function(data, status, headers, config) {
            //do something else
        });
    };
});

How can I test that ctls.save calls myFactory.save when the post was successful?

Aucun commentaire:

Enregistrer un commentaire