LoginSignup
33
24

More than 5 years have passed since last update.

Rails Tutorial のテストを RSpec で行う

Posted at

2週目以降を RSpec でやってみては!?

Rails Tutorial のテストを RSpec でやってみました (まだ途中までだけど)。
元々の Tutorial は mini test ですが、実際の現場では RSpec が使われる方が多いような気がしているので、
2週目以降は RSpec を使ってみるというのはありかなと思います。

導入と最初の方のテストを書いてみたので、ご参考に。

RSpec 導入編

RSpec 導入の参考記事

https://qiita.com/shizuma/items/8221544601aa3d0770d2
https://qiita.com/barasixi/items/db1fe162f53c5ddacde0

ざっくり導入手順

Gemfile 修正

rspec-railsfactory_bot_rails を導入します。
今回書いた中では、まだ factory_bot は活躍しません。。。

group :development, :test do
  gem 'sqlite3', '1.3.13'
  gem 'byebug',  '9.0.6', platform: :mri
  gem "rspec-rails"
  gem "factory_bot_rails"
end

インストール

$ bundle install
$ rails generate rspec:install

実行

$ bundle exec rspec                                                                       

実行結果

テストがないので、実行できるかの確認だけとなります。

No examples found.


Finished in 0.00038 seconds (files took 0.10134 seconds to load)
0 examples, 0 failures

第3章のテストを RSpec で書く

Tutorial に合わせて、Controller のテストを書く。

リスト 3.15: Aboutページのテスト (red)

spec/controllers/static_pages_controller_spec.rb
require 'rails_helper'

RSpec.describe StaticPagesController, type: :controller do

  (省略)

  describe "GET #about" do
    it "returns http success" do
      get :about
      expect(response).to have_http_status(:success)
    end
  end
end

失敗の確認

$ bundle exec rspec                                                                       

Failures:

  1) StaticPagesController GET #about returns http success
     Failure/Error: get :about

     ActionController::UrlGenerationError:
       No route matches {:action=>"about", :controller=>"static_pages"}
     # ./spec/controllers/static_pages_controller_spec.rb:20:in `block (3 levels) in <top (required)>'

リスト 3.24: StaticPagesコントローラのタイトルをテストする (red)

チュートリアルに沿って書く。

spec/controllers/static_pages_controller_spec.rb
require 'rails_helper'

RSpec.describe StaticPagesController, type: :controller do
  render_views

  describe "GET #home" do
    it "returns http success" do
      get :home
      expect(response).to have_http_status(:success)
      assert_select "title", "Home | Ruby on Rails Tutorial Sample App"
    end
  end

  (省略)
end

controller のテストですが、表示内容も確認したいので、render_views が必要。
render_views を書かないと、下記のようなエラーが発生するので注意。

     NoMethodError:
       undefined method `document' for nil:NilClass

リスト 3.30: 基本タイトルを使ったStaticPagesコントローラのテスト (green)

インスタンス変数ではなく、let を使用してみる。

spec/controllers/static_pages_controller_spec.rb
require 'rails_helper'

RSpec.describe StaticPagesController, type: :controller do
  render_views

  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
end

こんな感じで Rails Tutorial を RSpec で進めていければ良いかと思います。

33
24
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
33
24