Forum: RSpec How to spec a Rails helper method

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.
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick Hoffman (nickh)
on 2008-11-24 04:06
(Received via mailing list)
I'm trying to write specs for a helper method that I'm creating, but
my specs are failing to find the helper method

# app/helpers/properties_helper.rb
module PropertiesHelper
   def format_utilities(utilities)
   end
end

# spec/helpers/properties_helper_spec.rb
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe PropertiesHelper do
   describe '#format_utilities' do
     it 'should format a utilities value' do
       format_utilities(nil).should == 'Unknown'
     end
   end
end

The error that's occuring is:

NoMethodError in 'PropertiesHelper#format_utilities should format a
utilities value'
undefined method `format_utilities' for
#<
Spec
::Rails::Example::HelperExampleGroup::Subclass_1::Subclass_1:0x22a121c>
/Users/nick/src/housing-rentals/vendor/plugins/factories-and-workers/
lib/factories-and-workers/factory_builder.rb:36:in `method_missing'
./spec/helpers/properties_helper_spec.rb:7:
script/spec:4:

As far as I can tell, my specs match what was recommended back in
March 2008 in this thread:
   http://www.ruby-forum.com/topic/145723

Any suggestions?
-Nick
F86901feca747abbb5c6c020362ef2e7?d=identicon&s=25 Zach Dennis (zdennis)
on 2008-11-24 14:44
(Received via mailing list)
On Fri, Nov 21, 2008 at 1:18 PM, Nick Hoffman <nick@deadorange.com>
wrote:
> require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
>
> describe PropertiesHelper do
>  describe '#format_utilities' do
>    it 'should format a utilities value' do
>      format_utilities(nil).should == 'Unknown'
>    end
>  end
> end

Try:  helper.format_utilities

When describing a module RSpec will include that module into an object
accessible via the helper method. It used to work like you are
expressing your code, but no longer it does. ;)

Zach

> script/spec:4:
> http://rubyforge.org/mailman/listinfo/rspec-users
>



--
Zach Dennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com
881b7dd91c0d9287aea5bc505c10a15a?d=identicon&s=25 Ryan Briones (Guest)
on 2008-11-24 16:51
(Received via mailing list)
I'm not sure what version it was, but Rails helper modules are no longer
included implicitly in helper specs. [1]

You should rewrite your spec as:

describe PropertiesHelper do
 describe '#format_utilities' do
   it 'should format a utilities value' do
     helper.format_utilities(nil).should == 'Unknown'
   end
 end
end

or

describe PropertiesHelper do
 include PropertiesHelper
 describe '#format_utilities' do
   it 'should format a utilities value' do
     format_utilities(nil).should == 'Unknown'
   end
 end
end

[1]
http://blog.davidchelimsky.net/2008/5/29/rspec-wav...
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2008-11-24 16:57
(Received via mailing list)
On 21 Nov 2008, at 18:18, Nick Hoffman wrote:

> require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
>
> describe PropertiesHelper do
>  describe '#format_utilities' do
>    it 'should format a utilities value' do
>      format_utilities(nil).should == 'Unknown'
Here, try
       helper.format_utilities(nil).should == 'Unknown'
> #<
>  http://www.ruby-forum.com/topic/145723
I don't know about that thread - I wasn't using RSpec at the time, but
certainly the way I've always done it is use HelperExampleGroup.helper
to call the helper module. This is backed up by the docs:

http://rspec.info/documentation/rails/writing/helpers.html

HTH,
Matt
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2008-11-24 17:00
(Received via mailing list)
Have a look at the change to no longer implicitly include modules:

http://blog.davidchelimsky.net/articles/2008/05/29...

Regards,
Craig
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-11-24 17:01
(Received via mailing list)
On Fri, Nov 21, 2008 at 12:18 PM, Nick Hoffman <nick@deadorange.com>
wrote:
> require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
>
> in this thread:
>  http://www.ruby-forum.com/topic/145723

March is a long time ago :)

Use helper. format_utilities ...

Read this http://rspec.info/documentation/rails/writing/helpers.html
and if you're still unsure feel free to write back.

Cheers,
David
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick Hoffman (nickh)
on 2008-11-24 17:06
(Received via mailing list)
On 2008-11-24, at 00:39, Zach Dennis wrote:
> Try:  helper.format_utilities


Thanks everyone! I should have searched the API rather than Google.
I'll do that next time.

Cheers,
Nick
944f769c99deff7aa8bc3b5b93830b7a?d=identicon&s=25 Scott Taylor (Guest)
on 2008-11-24 17:11
(Received via mailing list)
On Nov 21, 2008, at 1:18 PM, Nick Hoffman wrote:

> require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
>
> describe PropertiesHelper do
>  describe '#format_utilities' do
>    it 'should format a utilities value' do
>      format_utilities(nil).should == 'Unknown'
>    end
>  end
> end
>
> The error that's occuring is:


Helpers are no longer automagically mixed into example groups.  Use
the "helper" object (helper.format_uttilities)

Scott
This topic is locked and can not be replied to.