0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【RSpec】Ruby on Rails チュートリアル「第5章」のテストを RSpec で書いてみた

Posted at

はじめに

こちらは前回の続きとなります。

【RSpec】Ruby on Rails チュートリアル「第4章」のテストを RSpec で書いてみた
https://qiita.com/t-nagaoka/items/4565a453596dfa0b0e3e

対象者

  • Ruby on Rails チュートリアルのテストを Rspec で実施予定、または実施してみたい人
  • Ruby on Rails チュートリアル「第5章」のテストを Rspec で書きたいけど、書き方が分からない人

テストコード

実際にテストコードを書き換えた結果が下記になります。

Minitest

test/controllers/static_pages_controller_test.rb
class StaticPagesControllerTest < ActionDispatch::IntegrationTest

  test "should get home" do
    get root_path
    assert_response :success
    assert_select "title", "Ruby on Rails Tutorial Sample App"
  end

  test "should get help" do
    get help_path
    assert_response :success
    assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
  end

  test "should get about" do
    get about_path
    assert_response :success
    assert_select "title", "About | Ruby on Rails Tutorial Sample App"
  end

  test "should get contact" do
    get contact_path
    assert_response :success
    assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
  end
end
test/integration/site_layout_test.rb
require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest

  test "layout links" do
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", help_path
    assert_select "a[href=?]", about_path
    assert_select "a[href=?]", contact_path
    get contact_path
    assert_select "title", full_title("Contact")
    get signup_path
    assert_select "title", full_title("Sign up")
  end
end
test/helpers/application_helper_test.rb
require 'test_helper'

class ApplicationHelperTest < ActionView::TestCase
  test "full title helper" do
    assert_equal full_title,         "Ruby on Rails Tutorial Sample App"
    assert_equal full_title("Help"), "Help | Ruby on Rails Tutorial Sample App"
  end
end
test/controllers/users_controller_test.rb
class UsersControllerTest < ActionDispatch::IntegrationTest

  test "should get new" do
    get signup_path
    assert_response :success
  end
end

RSpec

spec/requests/static_pages_spec.rb
require "rails_helper"

RSpec.describe "StaticPages", type: :request do
  describe "Home page" do
    it "should get root" do
      visit root_path
      expect(page.status_code).to eq(200)
      expect(page).to have_title full_title
      expect(page).not_to have_title "Home |"
    end
  end

  describe "Help page" do
    it "should get help" do
      visit help_path
      expect(page.status_code).to eq(200)
      expect(page).to have_title full_title("Help")
    end
  end

  describe "About page" do
    it "should get about" do
      visit about_path
      expect(page.status_code).to eq(200)
      expect(page).to have_title full_title("About")
    end
  end

  describe "Contact page" do
    it "should get contact" do
      visit contact_path
      expect(page.status_code).to eq(200)
      expect(page).to have_title full_title("Contact")
    end
  end
end
spec/features/site_layout_spec.rb
require 'rails_helper'

RSpec.describe "SiteLayoutTest", type: :feature do
  before { visit root_path }
  subject { page }
  scenario "layout links" do
    is_expected.to have_link nil, href: root_path, count: 2
    is_expected.to have_link nil, href: help_path
    is_expected.to have_link nil, href: about_path
    is_expected.to have_link nil, href: contact_path
    visit contact_path
    expect(page).to have_title full_title("Contact")
    visit signup_path
    expect(page).to have_title full_title("Sign up")
  end
end
spec/helpers/application_helper_spec.rb
require "rails_helper"

RSpec.describe ApplicationHelper, type: :helper do
  describe "full_title" do
    context 'no argument' do
      it 'return 「Ruby on Rails Tutorial Sample App」' do
        expect(full_title).to eq("Ruby on Rails Tutorial Sample App")
      end
    end
    context 'argument is "Help"' do
      it 'return 「Help | Ruby on Rails Tutorial Sample App」' do
        expect(full_title("Help")).to eq("Help | Ruby on Rails Tutorial Sample App")
      end
    end
  end
end
spec/requests/users_controller_spec.rb
require 'rails_helper'

RSpec.describe UsersController, type: :request do
  describe "GET #new" do
    it "returns http success" do
      get signup_path
      expect(response).to have_http_status(:success)
    end
  end
end

###ポイント
full_title ヘルパーを使用するためには、以下のように rails_helper.rb で ApplicationHelper を読み込む必要があります。

spec/rails_helper.rb
RSpec.configure do |config|
  include ApplicationHelper

##次回
「第6章」のテストコードを RSpec に書き換える予定です。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?