My promise does not call then () on a karma test

2

I have a problem making mocks with promises for a test with karma:

When I execute the "resolve (value)" of my promise, it does not jump to the then , does nothing, does not go to the "error", does not go to the "resolve" and the test is it remains in OK, (the objective of the test is simply to pass all the functionality without giving errors)

Here is the code that will be executed (I had to delete code but the problem is understood):

getLoadData() {
        let vm = this,
            url = ...;
        let _search = {
            ...
        };

        return vm.httpCacheGett(url,_search).then(getDataComplete, getDataFailed);

        function getDataComplete ( res ) : Object {
            console.log(7)
            return "todo ok"

        }

        function getDataFailed( err : any ) : Object {
            return "todo mal"
        }
    }

Here is the test code:

it('con error ', () => {
                linesDashboardServ.httpCacheGett = function(a,c){
                    console.log(2); 
                    var defered = $q.defer();
                    var promise = defered.promise;

                    setTimeout(function(){
                        console.log(3); 
                        defered.resolve([{1:1}]);
                    }, 1);
                    return promise;
                }; 


                linesDashboardServ.getLoadData("1");  

            }); 

What I do not understand is because it does not fit into the "console.log (7)" because it shows it to me in "console.log (3), so I should run the resolve ()

Do you know what it can be?

    
asked by borja ortiz llamas 06.06.2018 в 17:22
source

1 answer

1

Although how you handle it can be done, it may be more confusing to give an answer with that type of code, modify the code a bit so that it can be read better.

function httpCacheGett(a,c){
  console.log(2); 
  return new Promise((resolve, reject) =>
  {
    setTimeout(function(){
      console.log(3); 
      resolve([{1:1}]);
    }, 1);
  });
} 
let url = "";
let _search = ""
httpCacheGett(url,_search).then(getDataComplete, getDataFailed);


function getDataComplete ( res ) {
  console.log(7)
  return "todo ok"
  
}

function getDataFailed( err)  {
  return "todo mal"
}
    
  

You can see how to use promises and other reading this question

    
answered by 06.06.2018 в 17:46