Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS testing : Jasmine mock callback

I have basic angular JS knowledge but I'm very new to Jasmine unit testing. My issue is the following :

I need to test a service method (method from myService) :

myService.method = function(args)
{
    var parameter = "myParam";
    anotherService.anotherMethod(parameter, function(result)
    {
        //Stuff to test using result
        if(result == "blabla")
            testFunction("param");

    });
};

How can I mock anotherService.anotherMethod to return a result and test the rest of myService.method ? I need to check that for example that testFunction has been called with "param" (with expect(myFunction)toHaveBeenCalledWith("param")).

Thansk for your help

like image 633
Komo Avatar asked Mar 17 '15 17:03

Komo


1 Answers

What I wanted to do was create a fake function for my AJAX call and use one its arguments :

spyOn(anotherService, 'anotherMethod').and.CallFake(function(){
  //Get args : fake result of AJAX call and callback
  var fakeResult = arguments[0];
  var callback = arguments[1];
  return callback(fakeResult);
});
like image 125
Komo Avatar answered Sep 22 '22 11:09

Komo