mardi 5 mai 2015

Karma not calling private function inside angular timeout

I have controller which calls back the private function after few seconds of time out. Controller works fine. When I am trying to write unit test cases using karma with jasmine framework, it is not calling my private function inside timeout.

I used timeout.flush() which is going inside the timeout, but not calling the private function. Below is my controller code.

var counter = 0;
foo(1000);
function foo(timeout){
     $http({
          method: 'GET',
          timeout : timeout,
          url : "URL to get data"
     }).success(function(res){

     }).error(function(err){
          callinterval(0);
     });
}

function callinterval(code){
     if(counter < 3){
        $timeout(function(){
            foo(5000);
            counter++;
        }, 5000);
     }else{
        $("#errorModal").modal(modalOption);
     }
}

And my karma code goes like this

it('Test backend connection 505 failure', function() {
    console.log("Test backend connection 505 failure");
    var controller = createController();
    var errorMessage='';

    try {
        // set the HTML response status to 500, service failure
        httpBackend.when('GET','URL to get data')
            .respond(500);

        httpBackend.flush();
        timeout.flush();
    } catch(err) {
        errorMessage = err.message
        console.log(errorMessage);
    }

    expect(errorMessage).toMatch("errorModal");
});

If I make counter equal to 3 in controller, then I was able to pass this test. i.e., karma is able to pass the function in timeout if counter equal to 3. if counter is less than 3 then function foo is not calling.

Aucun commentaire:

Enregistrer un commentaire