mercredi 2 décembre 2015

Jasmine spyOn objects inside anonymous callback function

I need to unit test a controller functionsaveSettings() as shown below. I want to test if Session.setUrl() is being called or not on success. How can I achieve that?

angular.module("test.controllers")
    .controller('LoginCtrl', function(Validator, Session, Notify){

        $scope.apiUrl = "http://localhost:8080/web-server/";

        $scope.saveSettings = function () {

             Validator.validateUrl($scope.apiUrl, 
                  function (url) { // success callback function
                      Session.setUrl(url);
                  }, 
                  function (error) { // failure callback function
                      Notify.alert('' + error);
                  }
            );
        };
    });

And the service is defined as follows:

angular.module('test.services')
  .service('Validator', function () {

      this.validateUrl = function(url, success, error) {

          if ( !url ) {

              error("URL not found!");
          } else {

               if (url.startsWith('http') && !url.startsWith('https')) {
                   url = url.replace('http', 'https');
               }
               success(url);
          }
      };
  });

Aucun commentaire:

Enregistrer un commentaire