Javascript jasmine angularjs测试结果不一致
当运行我的单元测试时,有时一切正常,有时某个控制器的随机测试失败,原因我看不出来 故障报告如下:Javascript jasmine angularjs测试结果不一致,javascript,arrays,angularjs,unit-testing,recursion,Javascript,Arrays,Angularjs,Unit Testing,Recursion,当运行我的单元测试时,有时一切正常,有时某个控制器的随机测试失败,原因我看不出来 故障报告如下: Expected spy exec to have been called with [ Object({}) ] but actual calls were [ Object({}) ] var array = [{id:1}, {id:2}, {id:3}]; //first check the entire array //then process the entire array //t
Expected spy exec to have been called with [ Object({}) ] but actual calls were [ Object({}) ]
var array = [{id:1}, {id:2}, {id:3}];
//first check the entire array
//then process the entire array
//then do something after the entire array is processed.
checkArray(0, array, object).then(function(){
processArray(0, array, object).then(function() {
doSomething(object);
});
});
function checkArray(index, array, object) {
return $q(function(resolve) {
var record = array[index];
//object is altered in doSomeStuff
doSomeStuff(record, object).then(function(){
if(++index !== array.length) {
return resolve(checkArray(index, array, object));
} else {
return resolve(true);
}
});
});
});
我真的找不到预期呼叫和实际呼叫之间的任何差异。
(使用比较工具)
此控制器与其他控制器的区别在于它包含递归。
它包含一个返回数组的数据源,对于该数组中的每个项,都会执行异步代码,类似于:
Expected spy exec to have been called with [ Object({}) ] but actual calls were [ Object({}) ]
var array = [{id:1}, {id:2}, {id:3}];
//first check the entire array
//then process the entire array
//then do something after the entire array is processed.
checkArray(0, array, object).then(function(){
processArray(0, array, object).then(function() {
doSomething(object);
});
});
function checkArray(index, array, object) {
return $q(function(resolve) {
var record = array[index];
//object is altered in doSomeStuff
doSomeStuff(record, object).then(function(){
if(++index !== array.length) {
return resolve(checkArray(index, array, object));
} else {
return resolve(true);
}
});
});
});
实际代码可能会中途停止对调用错误的函数执行数组检查或处理,在这种情况下,将显示错误弹出窗口,并在单元测试中比较对象的最终状态
但是,在同一个控制器中也有测试再次失败(没有原因?),这些测试没有使用这些递归执行的函数
然而,这些间谍实际上是用一个在调用时略有不同的对象调用的。但是,Jasmine不会在调用时保持对象的状态,而是报告对象的最终状态。
(没关系,只是没有执行好,但我不在乎)
代码按需运行,但是我也希望测试能够在没有假想错误的情况下始终如一地运行。
如何防止这些错误突然出现
(我看不到其他与其他控制器真正不同的东西,因此我认为这种递归是我的问题的原因)
当我通过将数组大小设置为1来消除递归时,仍然会得到不一致的结果。我的对象包含一个新的Date()对象 因为秒差小于0.5秒,所以它没有显示为差,在许多情况下,时间差小于一毫秒,因此预期对象和实际对象的结果是相同的 通过使用一些随机字符串替换日期对象,测试始终如预期的那样成功