Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

doc(tutorial): Fix confusing test filter injection #1843

Closed
wants to merge 1 commit into from

Conversation

shairez
Copy link
Contributor

@shairez shairez commented Jan 19, 2013

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?)

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?)
@shairez
Copy link
Contributor Author

shairez commented Jan 19, 2013

If this fix is correct, we should also change the file on:
https://github.com/angular/angular-phonecat/blob/f37a97c3dffec7e9fee034732d6f874f43216615/test/unit/filtersSpec.js

Just let me know and I'll update it.
Thanks,
Shai

@pkozlowski-opensource
Copy link
Member

@shairez filters are not the same as controllers or directives but rather regular factories registered with the Filter suffix:
https://github.com/angular/angular.js/blob/master/src/ng/filter.js#L82

In practice it means that filters are singletons and you can inject them using 2 different methods. Given a filter with the name somename you can inject:

  • somenameFilter, or
  • $filter service and do: $filter('somename')

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.

@shairez
Copy link
Contributor Author

shairez commented Jan 26, 2013

@pkozlowski-opensource - Thanks! I wasn't aware of this convention.
Maybe we should add it to the guide: http://docs.angularjs.org/guide/dev_guide.templates.filters ?

@Ghoughpteighbteau
Copy link

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.

@caiges
Copy link

caiges commented Jan 22, 2014

@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.

@caiges
Copy link

caiges commented Jan 22, 2014

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.

@pabramowitsch
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants