LoginSignup
3
5

More than 5 years have passed since last update.

heavens_doorを使ってみた

Last updated at Posted at 2019-02-28

はじめに

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

実際のデモ

実際にはこんな感じで動作します

heavens_door.gif

なお、実際に作ったものはこちら

チュートリアル

rails new

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

rails new heavens_door

capybaraの導入

次に、capybaraを導入します

Gemfile
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を導入したいと思います!

Gemfileheavens_doorを追加します

Gemfile
gem 'heavens_door', group: :development

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

bundle install

heavens_doorを使う

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

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

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にクリップしたコードを貼り付けます

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です!

参考

amatsuda/heavens_door

teamcapybara/capybara#using-capybara-with-minitest

3
5
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
3
5