[rspec-users] newbie: need help to write the spec for helper

aslak hellesoy aslak.hellesoy at gmail.com
Wed Dec 31 11:11:05 EST 2008


On Wed, Dec 31, 2008 at 5:02 PM, David Chelimsky <dchelimsky at gmail.com>wrote:

> On Wed, Dec 31, 2008 at 9:49 AM, aslak hellesoy
> <aslak.hellesoy at gmail.com> wrote:
> >
> >
> > On Wed, Dec 31, 2008 at 4:21 PM, David Chelimsky <dchelimsky at gmail.com>
> > wrote:
> >>
> >> On Wed, Dec 31, 2008 at 8:00 AM, Nasir Jamal <nas35_in at yahoo.com>
> wrote:
> >> > Hi,
> >> >
> >> > I am a rspec newbie, can anyone guide me on how to write a spec for
> the
> >> > below helper.
> >> >
> >> > module MyHelper
> >> >  def test
> >> >    link_to('MyLink', resources_path) if @categories || @sub_categories
> >> >  end
> >> > end
> >> >
> >> > @categories is an instance of Category model
> >> > @sub_categories is an instance of SubCategory model
> >>
> >> Take a look at
> http://rspec.info/documentation/rails/writing/helpers.html.
> >> You can use assigns[:categories] and assigns[:sub_categories] to make
> >> the necessary data available to the helper.
> >
> > Technically you can do it that way, but personally I don't recommend that
> > approach in most cases. Testing modules is similar to testing private
> > methods, and the general advice is: Don't do it.
> >
> > Instead, test module methods and private methods indirectly via the
> > class/object that uses them. For modules this means: Write a spec for a
> > class that includes the module (in Rails this is a controller or view).
>
> So do you recommend never doing helper specs?


I never said never :-) Here is my manifesto styled take on this:

"I favour testing directly accessible APIs over indirectly accessible ones."

In Rails, I usually try to write a spec against a controller or view before
I resort to a helper spec.

Aslak


>
> >
> > Aslak
> >
> >>
> >> HTH,
> >> David
> >>
> >> >
> >> >
> >> >
> >> >
> >> > _______________________________________________
> >> > rspec-users mailing list
> >> > rspec-users at rubyforge.org
> >> > http://rubyforge.org/mailman/listinfo/rspec-users
> >> >
> >> _______________________________________________
> >> rspec-users mailing list
> >> rspec-users at rubyforge.org
> >> http://rubyforge.org/mailman/listinfo/rspec-users
> >
> >
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
> >
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081231/a650f340/attachment.html>


More information about the rspec-users mailing list