アプリケーションヘルパーのテスト方法_Railsチュートリアル5章より

Rails tutorialの第5章内にありました、アプリケーションヘルパーのテスト方法についてまとめました。

アプリケーションヘルパーテスト例

例えばtitleタグにapplication_helper.rbを使用しているとします。
総合テストでtitleタグが存在するかを実施するためは test環境でもApplicationヘルパーを使えるようにします。 test/test_helper.rb

  include ApplicationHelper

あとはテストコードを書けばいい感じです

ユニットテスト

ユニットテストを書いていきます
test/helpersapplication_helper_test.rbを作成します

require 'test_helper'

class ApplicationHelperTest < ActionView::TestCase

  def setup
    @base_title = "Ruby on Rails Tutorial Sample App"
  end

  test "full title helper" do
    assert_equal full_title, @base_title
    assert_equal full_title("Help"), "Help | #{@base_title}"
  end
end

あとは実行していきます

$ rails t

総合テスト

次に総合テストを追加します

test/integration/site_layout_test.rb

    get contact_path
    assert_select "title", full_title("Contact")

あとは総合テストを実施します $rails test:integration