2
1

More than 1 year has passed since last update.

【RSpec】Railsチュートリアル第6版 第3章

Last updated at Posted at 2021-10-29

はじめに

Railsチュートリアル第6版ではテストフレームワークとしてMinitestが採用されているが、実際に現場で使われることの多いRSpecを用いて、第3章から第14章において作成するsample_appのテストを書いていく。
テストする内容自体は基本的にはチュートリアルの内容に沿って進めていく。
また、RSpecの書き方については、『Everyday Rails - RSpecによるRailsテスト入門』という書籍を参考にしている。

目次

事前準備

Gemfileにrspecを追加

Gemfile
group :development, :test do
  gem 'rspec-rails'
end

RSpecをインストール

$ rails generate rspec:install

ジェネレーターの設定を変更

config/application.rb
module App
  class Application < Rails::Application
    config.load_defaults 6.1
    config.generators do |g|
      g.test_framework :rspec,
      view_specs: false,
      helper_specs: false,
      routing_specs: false
    end
  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 static_pages_home_url
    assert_response :success
    assert_select "title", "Home | Ruby on Rails Tutorial Sample App"
  end

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

  test "should get about" do
    get static_pages_about_url
    assert_response :success
    assert_select "title", "About | Ruby on Rails Tutorial Sample App"
  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 /home" do
    it "returns http success" do
      get "/home"
      expect(response).to have_http_status(:success)
      assert_select "title", "Home | #{base_title}"
    end
  end

  describe "GET /help" do
    it "returns http success" do
      get "/help"
      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"
      expect(response).to have_http_status(:success)
      assert_select "title", "About | #{base_title}"
    end
  end

end

StaticPagesコントローラーを作成するとrequest specが自動で作成されている。
Rails5以降、コントローラー機能のテストはrequest specによる記述が推奨されているらしいのでそちらを使用。
また、@base_titleを定義していたdef setupletによる遅延読み込みで代用。

発生するエラーについて

第8章においても記述しているが、不明なエラーが発生した場合はこちらを参考にしていただきたい。

spec/rails_helper.rb内の

spec/rails.helper.rb
ENV['RAILS_ENV'] ||= 'test'

spec/rails.helper.rb
ENV['RAILS_ENV'] = 'test'

に変更する。

RSpecを作成した際、ENV['RAILS_ENV'] ||= 'test'が自動で記述され、RSpecのデフォルトの環境変数にはtestが適用されるはずが、なぜかRails.env="development"になっており、エラーが発生していた。
これにより、RSpecのデータベースに開発環境用のデータベースが使用されており、また通常テスト環境には適用されないCSRFチェックが働き、422エラーなどを発生させていた。
かなりハマってしまったので是非参考にしていただきたい。

2
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
2
1