During the execution of my unit tests, I often encounter a scenario where some random test(s) fail for a specific controller without any apparent reason.
The error messages typically look like this:
Expected spy exec to have been called with [ Object({}) ] but actual calls were [ Object({}) ]
Despite using comparison tools, I struggle to identify any differences between the expected and actual calls.
The distinctive feature of this particular controller is that it involves recursion. It includes a data-source that returns an array, and asynchronous code is executed for every item in that array, as shown below:
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);
}
});
});
});
The issue arises when the code stops midway through checking or processing the array due to an error being triggered by a function call. This results in an error popup, and the final state of objects is compared in the unit tests.
Surprisingly, there are also failing tests within this same controller (for unknown reasons) that do not involve these recursive functions.
These spies, however, are being called with slightly different objects at the time of execution. While Jasmine reports the final state of the object, I am more concerned about the discrepancies at the time of calling.
Although the code fulfills its intended functionality, I strive for consistent test results without encountering unexpected errors. How can I mitigate these issues?
(Apart from the recursion aspect, I haven't identified any major differences compared to other controllers, leading me to believe that recursion might be the root cause of these inconsistencies.) Even when reducing the array size to 1 to eliminate recursion, the inconsistent results persist.