RSpec: Using helpers in view specs

Posted . Visible to the public.

If an view spec Show archive.org snapshot crashes due to undefined helper methods, you can enable this option:

# config/application.rb
config.action_controller.include_all_helpers = true

If you cannot use this setting, your spec can include individual helper modules like this:

describe 'some view', type: :view do
  helper SomeHelper
  helper OtherHelper

  it 'renders' do
    render 'view_that_uses_helpers'
  end
end

Alternatively you can also explicitly include all helpers of a given controller like this:

describe 'some view', type: :view do
  before(:each) do
    view.extend(ApplicationController._helpers)
  end

  it 'renders' do
    render 'view_that_uses_helpers'
  end
end

And one more verbose variant:

describe 'some view', type: :view do
  ApplicationController.modules_for_helpers([:all]).each do |helper|
    helper helper
  end

  it 'renders' do
    render 'view_that_uses_helpers'
  end
end
Henning Koch
Last edit
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2024-06-10 14:42)