amol01 amol01 - 4 months ago 9
AngularJS Question

Several async calls in jasmine 1.3

I am trying to make two async call in one jasmine test suite.
The second call should wait until the first one is finished then make a call.
So simple setup:



it('async tests', function(){

runs(function() {
flagToServer = false;
flagFromServer = false;
value1 = 0;
value2 = 0;
dataToGet = "";
dataToSend = "";

setTimeout(function() {
flagFromServer = true;
data = getDataFromServer();
}, 500);
});
waitsFor(function() {
value1++;
return flag;
}, "The Value should be incremented", 750);

runs(function() {
expect(value1).toBeGreaterThan(0);
expect(data).toBe(expectedData);
});

//second async call to server;
runs(function() {
dataToSend = manipulate(dataToGet);

setTimeout(function() {
sendDataToServer(dataToSend);
flagToServer = true;
}, 500);
});

waitsFor(function() {
value2++;
return flagToServer;

});

runs(function() {
expect(value2).toBeGreaterThan(0);
expect(eventFromServer).toBe('got data');
});

});





Is it possible to do something above? I could not find a usage where several waitsFor/runs blocks are used together. Is it the right way to test several async calls one after another?

Answer

So after experimenting with our setup, I have got that it is indeed possible to use several runs and waitsfor in one suit. They will be executed in order they were defined.

run(function() {

runs(function() {
  async();
})
waitsFor(function() {
  return data;
}, "Data should have come", 2000);

runs(function() {
  expect(data).not.toBeEmpty();
  async(data);
})

waitsFor(function() {
  return manipulatedData;
});

runs(function() {
  expect(manipulatedData).not.toBeEmpty();
});

Each respective wiatsFor will wait for condition that should come from their respective run blocks or timeout.

Comments