LoginSignup
27
23

More than 3 years have passed since last update.

Rails チュートリアル(3章、4章、5章)をRSpecでテスト

Last updated at Posted at 2019-08-20

はじめに

Rails チュートリアルのテストを書いて見ました。

追加しました。
Rails チュートリアル(6章)をRSpecでテスト
Rails チュートリアル(7章)をRSpecでテスト
Rails チュートリアル(8章)をRSpecでテスト

Rails チュートリアル 3,4章

RSpecの導入

gemを入れます。

gemfile

group :development, :test do
  gem 'rspec-rails', '~> 3.8'
end
$ bundle install
$ rails generate rspec:install

DBを作成していなければ、rails db:createで作成してください。作ってなく警告が出ました。

RSpecの設定

以下を参考にしました。↓
RailsでRSpecの初期設定を行う際のテンプレートを作ってみる-Qiita

.rspec
--require rails_helper
--format documentation
config/application.rb
module SampleApp
  class Application < Rails::Application
    config.load_defaults 5.2
#----------ここから下を追加---------------------------
    config.generators do |g|
      g.test_framework :rspec,
                       helper_specs: false,
                       routing_specs: false,
                       view_specs: false,
                       controller_specs: false
    end
#----------ここまで-----------------------------------
    config.generators.system_tests = nil
  end
end

RSpecを利用したコントローラの機能テストは、Rails5からはrequest specで記述することが推奨されてるみたいなので、controller_specsも省きました。

以下を参考にしました。↓
Rails5でコントローラのテストをController specからRequest specに移行する-Qiita
everydayrails
Project: RSpec Rails 3.8

テストを書く(3章、4章)

static_pagesコントローラーのテスト

4章のテストを書いてます。元のテスト↓
4.1.2 カスタムヘルパー -Ruby on Rails チュートリアル

spec/requests/access_to_static_pages_spec.rb

require 'rails_helper'

RSpec.describe 'Access to static_pages', type: :request do
  context 'GET #home' do
    before { get root_path }
    it 'responds successfully' do
      expect(response).to have_http_status 200
    end
    it "has title 'Ruby on Rails Tutorial Sample App'" do
      expect(response.body).to include 'Ruby on Rails Tutorial Sample App'
      expect(response.body).to_not include '| Ruby on Rails Tutorial Sample App'
    end
  end
  context 'GET #help' do
    before { get help_path }
    it 'responds successfully' do
      expect(response).to have_http_status 200
    end
    it "has title 'Home | Ruby on Rails Tutorial Sample App'" do
      expect(response.body).to include 'Help | Ruby on Rails Tutorial Sample App'
    end
  end
  context 'GET #about' do
    before { get about_path } 
    it 'responds successfully' do
      expect(response).to have_http_status 200
    end
    it "has title 'Home | Ruby on Rails Tutorial Sample App'" do
      expect(response.body).to include 'About | Ruby on Rails Tutorial Sample App'
    end
  end
end

以上で3章と4章のテストが完了です。
5章で変更するルートはここで変更してしまってます。
また、contactアクションをつくることになってますが、省いてます。

request specのファイル名はなんて書けばいいかわからず、なんとなくで書いてしまいました。

Railsチュートリアル 5章

5章のルートのテストは4章でついで書いてしまったので、残りのレイアウトのテストを書きます。

system specの準備

gemfile
+ gem 'capybara'
+ gem 'webdrivers'
- #gem 'chromedriver-helper' #削除
$ bandle install
spec/spec_helper.rb

require 'capybara/rspec' #これも追加

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
#-------------ここから追加-----------------------------
  config.before(:each, type: :system) do
    driven_by :selenium_chrome_headless
  end
#-------------ここまで--------------------------------
end

最後にspecフォルダーにrequestsフォルダーを作り、その中にテストファイルを作ります。

テストを書く(5章)

site_layoutのテスト

ホームページのリンクのテストを書きます。

元のテスト↓
5.3.4 リンクのテスト -Ruby on Railsチュートリアル

systems/site_layout_spec.rb

require 'rails_helper'

RSpec.describe 'site layout', type: :system do
  context 'access to root_path' do
    before { visit root_path }
    subject { page }
    it 'has links sach as root_path, help_path and about_path' do
      is_expected.to have_link nil, href: root_path, count: 2
      is_expected.to have_link 'Help', href: help_path
      is_expected.to have_link 'About', href: about_path
    end
  end
end

一つのページにリンクがそろっているかのテストなので、itで分けませんでした。正しいかはわかりません。

application_helperのテスト

次に以前書いたタイトルテストをリファクタリングします。テストでもfull_titleヘルパーを使います。

元のテスト↓
演習 -Ruby on Railsチュートリアル

spec/spec_helper.rb

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
  config.before(:each, type: :system) do
    driven_by :selenium_chrome_headless
  end
  config.include ApplicationHelper #追加
end
spec/requests/access_to_static_pages_spec.rb

require 'rails_helper'

RSpec.describe 'Access to static_pages', type: :request do
  context 'GET #home' do
    before { get root_path }   
    it 'responds successfully' do
      expect(response).to have_http_status 200
    end
    it "has title 'Ruby on Rails Tutorial Sample App'" do
      expect(response.body).to include full_title('') #変更部分
      expect(response.body).to_not include '| Ruby on Rails Tutorial Sample App'
    end
  end

  context 'GET #help' do
    before { get help_path } 
    it 'responds successfully' do
      expect(response).to have_http_status 200
    end
    it "has title 'Home | Ruby on Rails Tutorial Sample App'" do
      expect(response.body).to include full_title('Help') #変更部分
    end
  end

  context 'GET #about' do
    before { get about_path }
    it 'responds successfully' do
      expect(response).to have_http_status 200
    end
    it "has title 'Home | Ruby on Rails Tutorial Sample App'" do
      expect(response.body).to include full_title('About') #変更部分
    end
  end
end

次にapplication_helperのテストを書きます。(full_titleヘルパーのテストです)
specフォルダーにhelpersフォルダーを作成し、その中にテストファイルを作ります。

helpers/application_helper_spec.rb

require 'rails_helper'

RSpec.describe ApplicationHelper, type: :helper do
  describe '#full_title' do
    it { expect(full_title('')).to eq 'Ruby on Rails Tutorial Sample App' }
    it { expect(full_title('Help')).to eq 'Help | Ruby on Rails Tutorial Sample App' }
  end
end

usersコントローラーのテスト

元のテスト↓
5.4.1 Usersコントローラ -Ruby on Railsチュートリアル

requests/access_to_users_spec.rb

require 'rails_helper'

RSpec.describe 'access to users', type: :request do 
  describe 'GET #new' do
    it 'responds successfully' do
      get signup_path
      expect(response).to have_http_status 200
    end
  end
end

site_layoutのテスト(signupページについて)

元のテスト↓
演習 -Usersコントローラ -Ruby on Railsチュートリアル

systems/site_layout_spec.rb

require 'rails_helper'

RSpec.describe 'site layout', type: :system do
  #上記省略

  context 'access to signup_path' do
    before { visit signup_path }
    subject { page }
    it "has 'Sign up' contens and includes 'Sign up' at title" do
      is_expected.to have_content 'Sign up'
      is_expected.to have_title full_title('Sign up')
    end
  end
end

ここまで5章が終わりです。

27
23
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
27
23