shreyansh
shreyansh

Reputation: 1687

$httpBackend.flush() needs to call on each test : Jasmin-AngularJs Unit Test

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

Upvotes: 1

Views: 5034

Answers (1)

Robin-Hoodie
Robin-Hoodie

Reputation: 4984

$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();
}

Upvotes: 1

Related Questions