Re: [rspec-users] [Rails] How to mock/stub link_to from a Rails helper spec?
On Aug 20, 3:02 pm, Mike Sassak msas...@gmail.com wrote: On Thu, Aug 20, 2009 at 3:00 PM, Ed Ruder ed.ru...@gmail.com wrote: All, In a Rails helper spec, how do I stub or mock the 'link_to' method? When i step into link_to, self.class is Spec::Rails::Example::HelperExampleGroup::Subclass_1::Subclass_5::Subclass_ 1. What I'm trying to do is stub link_to and check that its parameters are correct--i.e., that a helper method is properly constructing the parameters to link_to. The work-around (and what seems to me to be a more awkward approach) is to grep the output of the helper method for a link with a proper href property. Hi Ed, Verifying the output of the helper method isn't a work around at all. Rather, it sounds like exactly what you should be doing in this case. Think of the helper method as a black box--you only care what comes out, not, necessarily, how it got there. Verifying output in this way rather than the implementation makes your specs more resilient in the face of changes, (what would happen, for example, if the calling semantics of link_to changed in a newer versions of Rails?), and is one of the central tenets of test/behavior driven development. RSpec already has lots of methods to verify HTML, too. See the Expectations section athttp://rspec.info/rails/writing/views.htmlformore details. Mike Mike, Sounds OK. Thanks. Ed ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
[rspec-users] [Rails] How to mock/stub link_to from a Rails helper spec?
All, In a Rails helper spec, how do I stub or mock the 'link_to' method? When i step into link_to, self.class is Spec::Rails::Example::HelperExampleGroup::Subclass_1::Subclass_5::Subclass_1. What I'm trying to do is stub link_to and check that its parameters are correct--i.e., that a helper method is properly constructing the parameters to link_to. The work-around (and what seems to me to be a more awkward approach) is to grep the output of the helper method for a link with a proper href property. Thoughts? Ed ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
[rspec-users] Change in -l command line option when line number is inside a describe block?
All, I often run specs from inside of TextMate, using the RSpec bundle. When the selection is inside of an example, I can run just that example using the RSpec bundle's Run Single Example script, which I assume ends up calling spec with a -l command line option (line number). Until a few versions ago (I don't know precisely when and I'm using 1.1.12), with a selection that's inside a describe block (but outside of any specific example block), Run Single Example would run all of the examples inside of that describe block--a very nice feature that I used all the time. In 1.1.12, it runs just the examples at the root of the file, in the top-most describe block, regardless of where the selection is. Was this an intentional change? Or, should I open up a ticket to restore this behavior? Thanks. Ed ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
Re: [rspec-users] Routes helpers work differently from helper examples compared to page rendering
I don't think that ticket applies to my problem--as I read it, https://rspec.lighthouseapp.com/projects/5645/tickets/843 is about enhancing RSpec with additional routing tests/specs. My issue is that in the context of a helper spec, when calling route helpers (_url/_path methods) from a helper, a different Rails code path is executed, giving different results in some cases. This means that sometimes your helper specs pass when the helper in the wild doesn't do what's spec'd--you cannot trust your specs! Thoughts? Ed On Jul 4, 3:53 pm, David Chelimsky dchelim...@gmail.com wrote: On Sat, Jul 4, 2009 at 4:40 PM, Ed Rudered.ru...@gmail.com wrote: I have a helper that calls a Rails route _url method. In the context of a helper spec file, the route helper method ends up calling url_for in rails/actionpack/lib/action_controller/base.rb. In the context of rendering a page, the route helper method calls url_for in rails/ actionpack/lib/action_view/helpers/url_helper.rb. Though very similar, they can return different results, as I found out. Specifically, ActionController::Base#url_for doesn't do html escaping-- ActionView::Helpers::UrlHelper#url_for does, and has an additional parameter, :escape = true|false, to controller whether or not the escaping is done. This took some frustrating time to track down. Since the code path executed by helper examples is not the same as production, tests can produce invalid results. Also, some behavior cannot be tested, since the code generating the results is not being executed. Is this a bug in RSpec, or is there a correct way to test helpers that I'm missing? Not sure if this is directly related, but you may be interested in this ticket:https://rspec.lighthouseapp.com/projects/5645/tickets/843 ___ rspec-users mailing list rspec-us...@rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
[rspec-users] Routes helpers work differently from helper examples compared to page rendering
I have a helper that calls a Rails route _url method. In the context of a helper spec file, the route helper method ends up calling url_for in rails/actionpack/lib/action_controller/base.rb. In the context of rendering a page, the route helper method calls url_for in rails/ actionpack/lib/action_view/helpers/url_helper.rb. Though very similar, they can return different results, as I found out. Specifically, ActionController::Base#url_for doesn't do html escaping-- ActionView::Helpers::UrlHelper#url_for does, and has an additional parameter, :escape = true|false, to controller whether or not the escaping is done. This took some frustrating time to track down. Since the code path executed by helper examples is not the same as production, tests can produce invalid results. Also, some behavior cannot be tested, since the code generating the results is not being executed. Is this a bug in RSpec, or is there a correct way to test helpers that I'm missing? ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users
[rspec-users] Failing in TextMate but not in rake...
When I run a spec file from TextMate using the RSpec bundle's Run Examples command (either using command-R or the bundle menu item with the spec file open), I get an error dialog with the following text: Missing the Rails 2.0.2 gem. Please `gem install -v=2.0.2 rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed. - Running 'rake spec SPEC=same spec file relative path' succeeds. - This line is in config./environment.rb: RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION - Running 'gem list rails' from the command line lists 'rails (2.0.2)' - I'm running on OS X Leopard with a version of Ruby installed from source into /usr/local, per Dan Benjamin's excellent instructions (http://hivelogic.com/articles/2008/02/ruby-rails-leopard). 'which ruby' finds '/usr/local/bin/ruby', 'which gem' finds '/usr/local/bin/gem'. - In TextMate's Advanced | Shell Variables, TM_RUBY is defined to '/usr/local/bin/ruby' (w/o the quotes). I'm guessing that TextMate is finding the wrong ruby or gem environment somehow, but I can't figure out how, or how to correct it. Any suggestions would be appreciated! Thanks in advance. Ed ___ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users