In order to simulate the behavior of this function, I want to create a mock:
function getMetaData(key) {
var deferred = $q.defer();
var s3 = vm.initiateBucket();
var params = {
Bucket: constants.BUCKET_NAME,
Key: key
};
s3.headObject(params, function(error, data) {
if(error) {
deferred.reject(error);
}
else {
console.log(data);
deferred.resolve(data.Metadata);
}
});
return deferred.promise;
};
To create the mock version of this function, I'm utilizing spyOn like so:
spyOn(awsServices, 'getMetaData').and.callFake(function() {
console.log("MOCKED");
return $q.resolve({
key: "MOCKED_KEY",
description: "MOCK_DESCRIPTION",
views: 1
});
});
The spy functionality appears to be functioning correctly - the console log statement for "MOCKED" is being triggered. Here's the function that invokes getMetaData:
function getPicsRadius(){
//Ommitted
var promises = [];
promises.push(awsServices.getMetaData(momentsInStates[i].Key)
.then(function(metaData) {
console.log(metaData);
return {
key: metaData.key,
description: metaData.description,
views: metaData.views
};
}))
}
return promise.all(promises);
This function is invoked by another function using promise chaining:
function getNearbyMoments() {
//omitted
return calculateNearbyStates()
.then(getPicsByState)
.then(concatPics)
.then(getPicsWithinRadius).then(function(){
console.log("SHOULD LOG BUT IS NOT");
});
}
The console.log message at the end is not appearing as expected. The only error encountered relates to an Async timeout issue. The flow should go from getMetaData(MOCKED) -> getPicsRadius -> getNearbyMoments (not entering the then function). I suspect the problem may lie with the Promise.all implementation, as I don't usually encounter difficulties when spying on regular promises. Any thoughts or suggestions?