How to test application helper code with RSpec
-
You don’t have to explicitly include the
ApplicationHelpermodule(viainclude ApplicationHelper) in the relevant spec to test itSample Code in
application_helper.rbmodule ApplicationHelper def title return t("learner") unless content_for?(:title) "#{content_for(:title)} | #{t("learner")}" end endRspec code in
application_helper_spec.rbthat 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