How to include Rails Helpers on RSpec

后端 未结 2 1622
庸人自扰
庸人自扰 2020-12-13 18:02

I\'m trying to include some helpers to test with rspec but no luck.

What I did:

created a support/helpers.rb file under my spec fol

2条回答
  •  一个人的身影
    2020-12-13 18:06

    I normally include this code to require everything under my spec/support subdirectory once the Rails stack is available:

    Spork.prefork do
    
      # ...
    
      Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
    
      RSpec.configure do |config|
        config.include MyCustomHelper
    
        # ...
      end
    end
    

    Note that this will include MyCustomHelper in all example types (controllers, models, views, helpers, etc.). You can narrow that down by passing a :type parameter:

    config.include MyControllerHelper, :type => :controller
    

提交回复
热议问题