はじめに
Rails/Rubyのコミッターであるa_matsudaさんが制作されているheavens_doorのチュートリアル記事です
実際のデモ
実際にはこんな感じで動作します
なお、実際に作ったものはこちら
チュートリアル
rails new
まずは、rails newでheavens_doorを試すサンプルアプリを作ります
rails new heavens_door
capybaraの導入
次に、capybaraを導入します
gem 'capybara', '>= 2.15', group: :development
そのあと、bundle installを実行します
bundle install
テスト用CRUD作成
テストするためのCRUDを作成します
rails g scaffold post title content
その後、rails db:migrateを実行します
rails db:migrate RAILS_ENV=test
heavens_doorの導入
heavens_doorを導入したいと思います!
Gemfileにheavens_doorを追加します
gem 'heavens_door', group: :development
bundle installでgemをインストールします
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です!
