-
Notifications
You must be signed in to change notification settings - Fork 27.4k
$httpBackend API difficult to use to test multiple requests. (API solution proposal included) #13717
Comments
That's a lot of text. While I appreciate details, can you give a short version that mentions the main issue and your proposed solution? |
Yes, good call. Short version: Instead of
Allow us to do something like:
|
Okay, thanks for the short version. Since this seems basically to be a "cosmetic" change, I wonder if it would be better to make that available as a third-party module. Also, I think the original behavior has some merits, as in tests you want to mock the data / responses before actually making them, instead after the fact. |
So as I mentioned in my "long version," mocking the data / responses before actually making the requests is exactly what is difficult to work with. What are the merits that you see for the pre-defined model vs. responding to requests afterwards? I personally don't see any (or at least none that couldn't be easily solved with a helper function). Tbh, our tests have become so hairy trying to work with multiple http requests on pages, and we're constantly breaking tests by moving requests into different orderings in the model/service layer and what-not. But we still want the ability to check that all requests have been satisfied, so we must still use Btw, I'm not sure that this could be a third-party module, as everything seems pretty baked into the ng mock source code. Thoughts? |
Made changes according to results of PR review. Related angular#13717
After writing some ~1k tests using
$httpBackend
, I've come to the conclusion that the current fashion of requests needing to be "expected" first is difficult to work with. Couple reasons:Brief Description
In general, it would be better to have an API that accepts all requests generated from the source code, and allows you to flush specific requests with their data when needed.
Detailed Description
To provide some more details, examples, and use cases, here is a more detailed description of the above points.
It is difficult to write (and understand) tests that make multiple HTTP requests, especially if the first request needs to complete before the second one is made.
For example, here's roughly what I have for one test. This test checks source code which:
A. Calls an endpoint
/user
, and when that is done,B. Calls
/account-details
, and when that is done,C. Shows the information.
Example Source Code (please forgive the contrived example):
Test with current
expectGET()
:This test would be much more readable with something like
respondToGET()
:We are currently unable to test what happens if two HTTP requests complete in a different order with the current API. Currently, the order in which the requests are made are also the order in which the "server" responds.
Let's say we have a page or component that loads data for two sections in parallel. Each section should display its data as soon as the corresponding request is complete.
Again, a simpler API which would allow flexibility on which request is resolved first (and also results in shorter code) would be:
Proposed API
Here's what I'm thinking for an API:
To maintain backward compatibility with throwing errors for "unexpected requests," it seems like we'd need to put
$httpBackend
into an "accept all / respond" mode so we can respond to the requests later:Then we would flush requests when we need to with:
respondToGET( url, [headers], [keys] ).with( [statusCode], data )
respondToPOST( url, [data], [headers], [keys] ).with( [statusCode], data )
.Thoughts?
The text was updated successfully, but these errors were encountered: