My goal is to create a unit test that demonstrates the process of combining two promises using $q.all in Angular, and then confirming that both promises are resolved simultaneously.
describe("Application controller", function() {
var scope;
var controller;
beforeEach(module('my.namespace'));
//this suite can be removed.
describe("Application ", function() {
beforeEach(
inject(function ($rootScope, $controller,$q) {
scope = $rootScope.$new();
controller = $controller('Application', {
'$scope': scope
});
}));
it("should merge two promises into one", function (done) {
inject(function ($rootScope) {
setTimeout(function () {
$rootScope.$apply(function () {
var promiseOne = $q.defer(),
//promiseOneData;
promiseTwo = $q.defer();
//promiseTwoData
promiseOne.then(function(data){
promiseOne.resolve('promiseOne');
expect(1).toBe(1);
});
promiseTwo.then(function(data){
promiseTwoData.resolve('promiseTwo');
})
var allPromises = $q.all([promiseOne,promiseTwo]);
allPromises.then(function(data){
//data should contain an array of two empty elements for each promise
expect(data.length).toBe(2);
});
done();
});
}, 1000);
})
});
However, I encounter an error message:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
My intention is not to execute any actual HTTP requests here; rather, I simply want to ensure that two promises are resolved before merging them into a single promise. How can this be achieved with Angular and jasmine?