AngularJS测试:Jasmine模拟回调

Kom*_*omo 5 testing callback jasmine angularjs

我有基本的Angular JS知识,但是对Jasmine单元测试非常陌生。我的问题如下:

我需要测试一种服务方法(来自myService的方法):

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

    });
};
Run Code Online (Sandbox Code Playgroud)

我如何模拟anotherService.anotherMethod返回结果并测试myService.method的其余部分?我需要检查一下,例如testFunction是否已用“ param”调用(带有Expect(myFunction)toHaveBeenCalledWith(“ param”))。

坦斯克为您提供帮助

Kom*_*omo 7

我想做的是为我的AJAX调用创建一个伪函数,并使用其参数之一:

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);
});
Run Code Online (Sandbox Code Playgroud)