标签:angularjs jasmine unit test callback
今天写了一个function,里面涉及了两个callback,大概形式为function callbackFunction() { ServiceA.callbackA(data1, function(result1){ //success function callback ServiceA.callbackA(data2, function(result2{ return [result1, result2]; }, function(error){ console.log(error); }); }, function(error){ console.log(error); }; }
为这个function写jasmine unit test的时候遇到了问题。一开始我是这么写的:
首先mock一个ServiceA及callbackA
mockServiceA = jasmine.createSpyOn('ServiceA', ['callbackA']);
var data1 = 'data1'; var data2 = 'data2'; mockServiceA.callbackA.and.callFake(data1, function(){ success(data1); }, function(){}); mockServiceA.callbackA.and.callFake(data2, function(){ success(data2); }, function(){});
解决办法,定义一个result hash根据parameter来指定返回结果
var data1 = 'data1'; var data2 = 'data2'; var result = { data1: data1, data2: data2 } mockServiceA.callbackA.and.callFake(data, function(){ success(result.data); }, function(){});
Jasmine test for AngularJS nested callback
标签:angularjs jasmine unit test callback
原文地址:http://blog.csdn.net/fsw0723/article/details/39190927