1

My question is very simple unlike

 afterEach (function () {
        $httpBackend.verifyNoOutstandingExpectation ();
        $httpBackend.verifyNoOutstandingRequest ();
    });

why $httpBackend.flush() cant be placed inside afterEach(function(){} ?? Because when I have several test cases, every time I need to call it.

describe("test1", function(){
it('1', function(){
$httpBackend.flush()
})
it('2', function(){
$httpBackend.flush()
})
it('3', function(){
$httpBackend.flush()
})
})

Even I tried to put $httpBackend.flush() inside after each block , but I started getting error " Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. ".

Is there any other way there by I can have only one block to be automatically get executed after every function for $httpBackend.flush()

Thanks

shreyansh
  • 1,637
  • 4
  • 26
  • 46
  • can you add you code snippet? – Zamboney Mar 20 '16 at 10:21
  • 2
    The point of flush() is to make the backend send the responses to the requests it has received, and to be able to verify expectations on the response after. So calling flush after the test doesn't make much sense. – JB Nizet Mar 20 '16 at 10:56
  • Thank you for your explanation, but still Is there any way or trick we can avoid calling $httpBackend.flush() multiple times?? – shreyansh Mar 20 '16 at 11:06
  • This question really doesn't make sense. You **choose** to call flush when you need to call it, because you need to get the responses from the fake backend, because you need to make assertions on the responses. You seem to assume calling flush is something cumbersome that you have to do even if you don't want to, but that's not the case. Now if you posted a real test example and explain what you want to achieve in that test, we could help more. – JB Nizet Mar 20 '16 at 17:46

1 Answers1

1

$httpBackend.flush should be called in your tests, not after the test. It is a part of the test so it does not make sense executing it after the test.

flush simulates sending your request to the server after which your possible httpBackend.expectXX method will either fail or pass. There's also other conditions you might want to check on.

If you want to write a helper method to avoid calling it all the time you could write something like this for example:

function executeRequestAndFlush(requestToExecute) {
   requestToExecute();
   $httpBackend.flush();
}
Robin-Hoodie
  • 4,886
  • 4
  • 30
  • 63