http://dev.rubyonrails.org/ticket/3775 I''m hoping someone will pop up and tell me I''m simply daft with this ticket (and queries on IRC, and a prior post to the main Rails list), but I can''t get a peep out of anyone so far. The question is, how do I write a simple test for a helper method which calls ActionView methods itself. E.g., a foo_link() helper which calls link_to(). I noticed that Typo doesn''t test these, though it seems to have tests for everything else. Am I missing something, or is there a gap here in Rails'' testability? Best, Rick -- http://www.rickbradley.com MUPRN: 485 | conversion Spam is random email haiku | all about prior consent | for delivery.
On 2/9/06, Rick Bradley <rick@rickbradley.com> wrote:> > http://dev.rubyonrails.org/ticket/3775 > > I''m hoping someone will pop up and tell me I''m simply daft with this > ticket (and queries on IRC, and a prior post to the main Rails list), > but I can''t get a peep out of anyone so far. > > The question is, how do I write a simple test for a helper method which > calls ActionView methods itself. E.g., a foo_link() helper which calls > link_to(). I noticed that Typo doesn''t test these, though it seems to > have tests for everything else. > > Am I missing something, or is there a gap here in Rails'' testability? > > Best, > RickCheck out the ActionView tests. Here''s the tag helper tests, for example: http://dev.rubyonrails.org/browser/trunk/actionpack/test/template/tag_helper_test.rb The url_for tests require a controller, so you''ll see some of them creating fake controllers with the bare minimum defined. -- Rick Olson http://techno-weenie.net
Rick Bradley
2006-Feb-09 21:33 UTC
Re: #3775 - testing helpers which use ActionView methods
* Rick Olson (technoweenie@gmail.com) [060209 16:15]:> Check out the ActionView tests. Here''s the tag helper tests, for example: > > http://dev.rubyonrails.org/browser/trunk/actionpack/test/template/tag_helper_test.rb > > The url_for tests require a controller, so you''ll see some of them > creating fake controllers with the bare minimum defined.Rick, Thanks for the assistance! I guess my question is more about how one has to go about testing these sorts of helpers -- maybe it''s more a question of philosophy than feasability. For example, in our case, we had a developer trying to write a functional test for a helper which called link_to(). He ended up in the weeds and I started working with him and quickly came to the realization that we had two options (we were indeed declaring a bare-minimum controller for the test, as #3775 showed): (1) Stub the link_to() method so that the dependency problem ceases to exist. (2) Each time we get an error for a method that can''t be found, track down the method in the Rails framework and add a require/include to make it visible. While (2) is a reasonable method to test helpers that are written inside Rails itself (since they are expected to have some knowledge of the layout of the framework), it''s a bad Code Smell for tests of helpers in application code. Not only does this technique require ferreting out the location of dependent methods from the Rails class hierarchy, but it forces the tester to do white box testing on the helper method in the first place. The tester has to know that the helper is relying upon certain specific Rails calls to get its job done, and then has to load those specific pieces to get his test written. If the internal helper code changes, the require/include list in the test may well get out of date (more bad smell). If the layout of Rails changes in the future, tests will break because they will fall out of sync with the layout of whichever Rails modules the dependent methods needed in place. Anyway, when Rails is running and a helper is called the call succeeds and the developer never had to go around trying to pick out which Rails modules have to be included to make that helper run. Why should testing that helper suddenly require the developer to become familiar with the layout of the Rails guts? Another way: the helper tests are too damned coupled to the Rails guts. Granted, there''s probably more important things to worry about, but if I wanted to write a bunch of library inclusion lines for a snippet of code I''d go back to Java. Seems like there should be a Railsy way to just write the obvious black-box test and not have to fight the framework to get it done. Thanks, Rick -- http://www.rickbradley.com MUPRN: 284 | actually puts random email haiku | them in is an exercise | for the curious.
Maybe Matching Threads
- How do I test actionview helpers in the console?
- scope problems testing a helper method that calls link_to()
- Patch lovin'' for #2018? ("rake appdoc fails on Windows systems")
- Design flaw with Fixtures?
- HELP: Strange problem with breakpointer - stopped working