Forum: RSpec How to write a spec file for a helper

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C42c706fab23da0b6d3cd2cfbb8db27d?d=identicon&s=25 Salil Gaikwad (salil)
on 2009-04-13 17:09
How to write a spec file for a following helper

module ArtistsHelper

def round_to(x)
    (self * 10**x).round.to_f / 10**x
end

end

Regards

salil
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-04-13 17:23
(Received via mailing list)
On Mon, Apr 13, 2009 at 12:09 PM, Salil Gaikwad <lists@ruby-forum.com>
wrote:
> How to write a spec file for a following helper
>
> module ArtistsHelper
>
> def round_to(x)
>    (self * 10**x).round.to_f / 10**x
> end
>
> end

in spec/helpers/artists_helper_spec.rb:

describe AristsHelper do
  it "rounds to ...." do
    helper.round_to(xxx).should == yyy
  end
end

Cheers,
David
39100495c9937c39b2e0c704444e1b4a?d=identicon&s=25 Pat Maddox (Guest)
on 2009-04-13 17:28
(Received via mailing list)
On Mon, Apr 13, 2009 at 8:17 AM, David Chelimsky <dchelimsky@gmail.com>
wrote:
>
> in spec/helpers/artists_helper_spec.rb:
>
> describe AristsHelper do
>  it "rounds to ...." do
>    helper.round_to(xxx).should == yyy
>  end
> end

That's what I was going to say, but I'm not sure that it's a Rails
helper...the calculation rounds self.  So I'm a bit confused.

If this is a module that you're using to extend the behavior of
Numeric classes, just mix it in somewhere and write examples for the
class that got the mixin.

Pat
77a052f877809ac07e5d8b7d9413d547?d=identicon&s=25 Brandt Kurowski (Guest)
on 2009-04-14 17:17
(Received via mailing list)
On Apr 13, 11:26 am, Pat Maddox <pat.mad...@gmail.com> wrote:
> If this is a module that you're using to extend the behavior of
> Numeric classes, just mix it in somewhere and write examples for the
> class that got the mixin.

I prefer to test modules in isolation by mixing them into a stub in
the spec, in order to make it explicit that we're describing the
module itself (not some other class), and to avoid dependencies on
that other class. For example:

  describe ArtistsHelper do
    before do
      @artist = 123.456
      class << @artist
        include ArtistsHelper
      end
    end

    it "should round stuff" do
      @artist.round_to(1).should == 123.5
    end
  end

Granted, it looks a little awkward here since it looks like the module
is meant to be used with a Numeric, but in general I find this
approach to be pretty clean.

--
Brandt
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2009-04-15 10:28
(Received via mailing list)
On 14 Apr 2009, at 16:14, Brandt Kurowski wrote:

>  describe ArtistsHelper do
>  end
>
> Granted, it looks a little awkward here since it looks like the module
> is meant to be used with a Numeric

Isn't it? That's probably what I would infer if I read this code.

Is there any reason why you don't do this?

     @artist = Object.new.extend(ArtistHelper)


Matt Wynne
http://beta.songkick.com
http://blog.mattwynne.net
This topic is locked and can not be replied to.