How to test application helper code with RSpec
-
You don’t have to explicitly include the
ApplicationHelper
module(viainclude ApplicationHelper
) in the relevant spec to test itSample Code in
application_helper.rb
module ApplicationHelper def title return t("learner") unless content_for?(:title) "#{content_for(:title)} | #{t("learner")}" end end
Rspec code in
application_helper_spec.rb
that works:require 'rails_helper' RSpec.describe ApplicationHelper, type: :helper do describe '#title' do it 'returns page specific title in the context of the app name' do content_for(:title) { "Page Title" } expect(title).to eq("Page Title | #{I18n.t('learner')}") end it 'returns app name when page title is missing' do expect(title).to eq("#{I18n.t('learner')}") end end end