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 at
http://rspec.info/rails/writing/views.htmlfor more details.

Mike
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to