Imagine having a straightforward function that includes a for
statement:
public loadImages(images): void {
for (let image of images) {
this.imageLoader.load(image['url']);
}
}
When attempting to spy on imageLoader.load
, the testing process fails. However, if the function is adjusted to call load
outside the loop, the test succeeds.
it('should load images using the loader', inject([ImageService, ImageLoader], (
service: ImageService,
imageLoader: ImageLoader) => {
spyOn(imageLoader, 'load');
service.loadImages({ 'url': 'example.com/image1.jpg' });
expect(imageLoader.load).toHaveBeenCalled();
}));
Upon checking console logs, it appears that the loadImages
function is being executed, but the for
loop itself is not being entered. What could be causing this behavior?