-
Notifications
You must be signed in to change notification settings - Fork 27.4k
doc(tutorial): Fix confusing test filter injection #1843
Conversation
While doing some TDD, I've noticed that you can't really inject controllers, directives and filters. So I didn't understand how is this unit test passing, in my runner it throws an error. Shouldn't we use the $filter instead of asking directly the filter name? (like controllers?)
If this fix is correct, we should also change the file on: Just let me know and I'll update it. |
@shairez filters are not the same as controllers or directives but rather regular factories registered with the In practice it means that filters are singletons and you can inject them using 2 different methods. Given a filter with the name
Both forms are valid and both could be used in tests. You can have a look at this video for more info about filters testing: http://egghead.io/video/angularjs-testing-overview/ Going to close this PR as the current documentation is valid as far as I can tell. Please re-open if you disagree. |
@pkozlowski-opensource - Thanks! I wasn't aware of this convention. |
Are you guys really really sure this is obvious? John Lindquist's explanation of injecting filters goes like this: "Were going to inject out reverse filter into this: so it will "inject" [in to] a function that requires our reverse filter" He then types "reverseFilter", a keyword never once seen or mentioned before, and blows the minds of anyone who was paying attention. This is highly non-obvious behavior. At the very least this needs to be mentioned in the angular.mock.inject documentation, where people will actually look for information on "how to inject X" ideally, with an example. |
@pkozlowski-opensource, having just run into this myself while testing filters, it feels like it's something that needs to be documented. While the documentation may be valid, it's not clear what's going on without diving into the code. That said, the code is fairly clear (https://github.com/angular/angular.js/blob/master/src/ng/filter.js#L99) but this is one of those things that makes stepping into Angular challenging. Thanks for the clarification in any case. |
http://docs.angularjs.org/api/ng.$filterProvider -- There's more information on that page as well. The clarification for this issue is out there albeit not exactly apparent at first glance. |
Just started the tutorial myself and agree with the above comments about xxxFilter (as well as others describing the use of $httpBackEnd in unit tests) . A perfect example of the 'walk before you run' admonishment. The ramp up on Angular and its unit testing is already demanding, so an intro tutorial should avoid the use of clever, non obvious or insider information and stick to more obvious solutions. Shortcuts and tricks could be the topic of a follow-on advanced tutorial. |
While doing some TDD, I've noticed that you can't really inject controllers, directives and filters.
So Went over the tutorial and didn't understand how is this unit test passing,
Checked, and in my runner it throws an error.
Shouldn't we use the $filter instead of asking directly the filter name? (like controllers?)