0
0

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】Railsチュートリアル第6版 第5章

Last updated at Posted at 2021-10-30

#はじめに
Railsチュートリアル第6版のテストをRSpecで書き直していく。

###目次

#事前準備
Gemfileにsystem spec用のcapybaraを追加

Gemfile
group :test do
  gem 'capybara'
end

ファイル内容の変更

spec/support/capybara.rb
RSpec.configure do |config|
  config.before(:each, type: :system) do
    driven_by :rack_test
  end
  config.before(:each, type: :system, js: true) do
    driven_by :selenium_chrome_headless
  end
end

#Minitest

####StaticPagesコントローラーのテスト

test/controllers/static_pages_controller_test.rb
require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

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

  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
  end

end

#RSpec

####StaticPagesコントローラーのテスト

spec/requests/static_pages.rb
require 'rails_helper'

RSpec.describe "StaticPages", type: :request do

  let(:base_title) { "Ruby on Rails Tutorial Sample App" }

  describe "GET root" do
    it "returns http success" do
      get root_path
      expect(response).to have_http_status(:success)
      assert_select "title", "#{base_title}"
    end
  end

  describe "GET /help" do
    it "returns http success" do
      get help_path
      expect(response).to have_http_status(:success)
      assert_select "title", "Help | #{base_title}"
    end
  end

  describe "GET /about" do
    it "returns http success" do
      get about_path
      expect(response).to have_http_status(:success)
      assert_select "title", "About | #{base_title}"
    end
  end

  describe "GET /contact" do
    it "returns http success" do
      get contact_path
      expect(response).to have_http_status(:success)
      assert_select "title", "Contact | #{base_title}"
    end
  end

end

Contactページのテストを追加
名前付きルートを使用するよう変更

####レイアウトのリンクに対するテスト

spec/system/site_layout_spec.rb
require 'rails_helper'

RSpec.describe "SiteLayouts", type: :system do

  it "layout links" do
    visit root_path
    expect(page).to have_current_path "/"
    expect(page).to have_link href: root_path, count: 2
    expect(page).to have_link href: help_path
    expect(page).to have_link href: about_path
    expect(page).to have_link href: contact_path
  end

end

system specを作成し、レイアウトの各リンクをテスト。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?