MyFirstRails

Heavens Door サンプル

概要

Railsにこれから初めて触れる肩を対象にしたチュートリアルです

Rails/Rubyのコミッターであるa_matsudaさんが制作されているheavens_doorをRailsに導入するチュートリアルです

チュートリアル

Railsのひな型を作る

まずは、rails newheavens_doorを試すサンプルアプリを作ります

rails new heavens_door

次に、作成したRailsアプリのディレクトリに移動します

cd heavens_door

SQLite3のバージョン修正

先ほどのrails newsqlite3のインストールがエラーになっている場合は、以下のようにバージョンを指定してください

gem 'sqlite3', '1.3.13'

その後、bundle installを実行します

bundle install

テスト用CRUD作成

テストするためのCRUDを作成します

rails g scaffold post title content

その後、rails db:migrateを実行します

rails db:migrate RAILS_ENV=test

rails db:migrate RAILS_ENV=testでマイグレーションが実行できない場合は、Gemfilegem 'chromedriver-helper'をコメントアウトします

heavens_doorの導入

heavens_doorを導入したいと思います!

Gemfileheavens_doorを追加します

gem 'heavens_door', group: :development

bundle installgemをインストールします

bundle install

heavens_doorを使う

最後に、heavens_doorを使ってみましょう!

まず、test/intgration/posts_test.rbを作成し、以下のようにします

require 'capybara/rails'
require 'capybara/minitest'

class ActionDispatch::IntegrationTest
  include Capybara::DSL
  include Capybara::Minitest::Assertions

  def heavens_door
  end
end

その後、rails sでローカルサーバを起動し、localhost:3000/postsにアクセスします

rails s

あとは画面右上に表示されているボタンをクリックするとUI操作の記録がはじまります

適当に操作したあと、バインダーマークをクリックするとUIテストのコードがクリップされます

あとは、test/intgration/posts_test.rbにクリップしたコードを貼り付けます

require 'capybara/rails'
require 'capybara/minitest'

class ActionDispatch::IntegrationTest
  # Make the Capybara DSL available in all integration tests
  include Capybara::DSL
  # Make `assert_*` methods behave like Minitest assertions
  include Capybara::Minitest::Assertions

  # Reset sessions and driver between tests
  # Use super wherever this method is redefined in your individual test classes
  def heavens_door
    scenario 'GENERATED' do
        visit '/posts'
    
        click_link 'New Post'
    
        fill_in 'Title', with: 'test'
        fill_in 'Content', with: 'aaaaaaaaaaaaaaaaaaaaaaaaa'
        click_button 'Create Post'
    
        click_link 'Back'
    end    
  end
end

最後に、rails testを実行してください

rails test

テストが実行されていればOKです!

これでRailsアプリにHeavens Doorを導入できました!