angular / angular.io

Website for the Angular project (see github.com/angular/angular for the project repo)
https://angular.io
MIT License
1.03k stars 880 forks source link

documentation: describe relation of inject() and async() functions in testing #2678

Open JohannesRudolph opened 8 years ago

JohannesRudolph commented 8 years ago

I'm submitting a ... (check one with "x")

[ ] bug report => search github for a similar issue or PR before submitting [x] feature request [ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question Current behavior The documentation describes the use of inject() and async() separately, but not how (or if) they interact or which is the one that users should be using.

https://angular.io/docs/ts/latest/guide/testing.html https://angular.io/docs/ts/latest/guide/testing.html#!#inject https://angular.io/docs/ts/latest/guide/testing.html#!#async

I only found this thread about this topic, but am unsure if the information is even up to date: #5570

Apparently one can use inject() instead of async()? works in my tests at least, but that may be me....

Expected behavior Document the relationship between the two.

What is the motivation / use case for changing the behavior? I have async tests but need to get injected services. There's too many ways to do the same thing right now (inject, vs. TestBed.get vs. the slightly different DebugElement.injector.get)

wardbell commented 8 years ago

You cannot use inject instead of async ... they have different purposes. There is no relationship between the two and therefore nothing to document on that score. What led you to believe they are related? The inject function is always synchronous, btw.

The doc does explain the use of inject (always from the root injector) vs. debugElement.injector.get which gets the service from the injector (at whatever level) of the particular debugElement instance. It also explains WHY you would chose one vs. the other.

I'm not sure how to improve upon that. Your issue isn't helping us do so. Perhaps you can clarify?

JohannesRudolph commented 8 years ago

I've just discovered what I was missing here. My question was primarily about how to combine the two, since their use appears exclusive to me. However, the MockBackend API docs somewhat cleared it up for me, you can apparently inject an AsyncTestCompleter (which however is fully undocumented):

inject([AsyncTestCompleter], (async) => { ... async.done(); }

JohannesRudolph commented 8 years ago

... adding to my previous statement, the async() code docs also says:

Can be used to wrap an {@link inject} call.

 it('...', async(inject([AClass], (object) => {
   object.doSomething.then(() => {
     expect(...);
   })
 });

I'll leave it up to you to decide whether the official docs on angular.io should mention/explain this.