vendredi 22 janvier 2016

AngularJS Jasmine Test - how to skip over a request

We have a set of test cases which is common for many different forms. Some of the forms may trigger a lazy loader which would prefetch some data in their post section of the directive, so firing a HTTP GET.

In the common test's beforeEach, I thus have to handle it, else I get Unexpected request: GET /bla/requests/generateJson/something No more request expected

I added $httpBackend.when("GET",//generateJson/(.+)/).respond(); before scope.$digest(); in the test's beforeEach. Now, the error changes to

Error: Unflushed requests: 1

Adding $httpBackend.flush();

causes the test cases when the GET call is fired to pass, but all test cases which do not fire a GET, fail with error

Error: No pending request to flush !

How can I handle that? I just want this test to ignore the GET completely, whether it is present or not. Can I somehow determine if the GET was fired, and so react with flush?

Aucun commentaire:

Enregistrer un commentaire