Last Updated: February 25, 2016
·
3.588K
· mcansky

Using ActionView helpers from a spec

Still walking the old and used paths of rspec 1 and Rails 2.3 I've now battled with helpers ...

Turns out using ActionView helpers from a spec is not directly possible. If you look around stack overflow etc you'll find a bunch of threads talking about and how helper.some_helper will do the trick.

Alas it's not that simple.

Someone did publish something far more useful and precise : Roger Campos from Itnig : How to get access to rails view helpers from rspec support files .

Here is the trick : you need to define that helper method and include the helpers modules you want to use. Yep.

So in your spec (or the spec_helper) you need to add this :

def helper
  Helper.instance
end

class Helper
  include Singleton
  include ActionView::Helpers::NumberHelper
end

In this case we only include the ActionView::Helpers::NumberHelper helper, but you can add more.

Then to use an helper from there you just have to like this :

my_value.should == helper.number_to_human_size(10000)

That's it !

Have fun