Rails4/Rspec で View のテスト

Rails4/Rspec で View のテスト

テストに RSpec & Capybara を使うように変更

Gemfile に追記

group :development, :test do
  gem 'rspec-rails', '2.14.2'
end
 
group :test do
  gem 'selenium-webdriver', '2.41.0'
  gem 'capybara', '2.2.1'
end

使うように指定。test 系のコマンドの設定?

$ rbenv exec rails generate rspec:install

Capybara を設定

spec/spec_helper.rb

RSpec.configure do |config|
  # Capybara
  config.include Capybara::DSL
end

テストの雛形を生成する

$ rbenv exec bundle exec rails generate integration_test hoge_piyo

テストする

テストの実行

$ rbenv exec bundle exec rspec spec/requests/hoge_piyo.rb

該当のコンテンツが含まれているか

require 'spec_helper'
describe "hoge piyo" do
  describe "fuga" do
    it "should have the content 'ほげ'" do
      visit '/hoge_piyo/fuga'
      expect(page).to have_content('ふが') 
    end
  end
end

該当の文字がタイトルに含まれているか

require 'spec_helper'
describe "hoge piyo" do
  describe "fuga" do
    it "should have the title 'ふが'" do
      visit '/static_pages/home'
      expect(page).to have_title("ふが")
    end
  end
end
ruby/rails4/view_testing_by_rspec.txt · 最終更新: 2016-02-05 10:22 by ore