Ed Ruder
2009-Aug-20 19:00 UTC
[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
Ed Ruder
2009-Aug-21 21:51 UTC
[rspec-users] [Rails] How to mock/stub link_to from a Rails helper spec?
On Aug 20, 3:02?pm, Mike Sassak <msas... at gmail.com> wrote:> On Thu, Aug 20, 2009 at 3:00 PM, Ed Ruder <ed.ru... at 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. > > MikeMike, Sounds OK. Thanks. Ed