How to include Rails Helpers on RSpec

后端 未结 2 1617
庸人自扰
庸人自扰 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:05

    Include the Module you need directly in the spec file:

    include PostsHelper
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题