My current task involves testing an asynchronous code snippet structured like this:
randomService.doSomething().then(function() {
console.log('The operation has been completed!');
});
Interestingly, I've noticed that the success message is only displayed when enclosed within jasmine's runs
function, as shown below:
var isDone = false;
runs(function() {
randomService.doSomething().then(function(data) {
console.log('The operation has been completed!');
isDone = true;
});
});
waitsFor(function() {
return isDone;
}, 'Operation should be completed', 1000);
Initially, I thought that the purpose of waitsFor
was simply to delay the code execution. I believed I would only need to use it if there were additional code to be executed after the asynchronous call. Therefore, I assumed that there was no need for both runs
and waitsFor
in this scenario since there were no subsequent operations. This was the conclusion I drew from the discussion on this topic: What do jasmine runs and waitsFor actually do? It seems I may have misunderstood something along the way.
I am open to any insights or suggestions on this matter.
UPDATE: For a more detailed overview of the issue, refer to this Plunker: http://plnkr.co/edit/3qnuj5N9Thb2UdgoxYaD?p=preview
Observe that the first test consistently passes, while the second test fails, as expected. Furthermore, I should mention that this scenario involves the use of angularJS and Jasmine 1.3.