メモ
rails new
するときに、rails new sample_app --skip-test-unit
とすることで、通常生成される、testディレクトリを作成しないようにする。
下記のGemを追加する。
group :development, :test do gem 'rspec-rails', '2.13.1' end group :test do gem 'selenium-webdriver', '2.35.1' gem 'capybara', '2.1.0' end
Test::Unitの代わりにRSpecを使うように、Railsの設定を変更する。
$ rails generate rspec:install
結合テスト(request spec)を生成する
$ rails g integration_test static_pages
spec/requests/static_pages_spec.rbにテストを記述する。
require 'spec_helper' describe "Static pages" do describe "Home page" do it "should have the content 'Sample App'" do visit '/static_pages/home' expect(page).to have_content('Sample App') end end end
spec/spec_helper.rbにCapybaraのDSLを追加する。
RSpec.configure do |config| . . . config.include Capybara::DSL end
テストを実施する。
$ bundle exec rspec spec/requests/static_pages_spec.rb
もう少しスッキリとさせた例
full_title(”)はspec/support/utilities.rb(自動で読み込まれる)に記載されている。
require 'spec_helper' describe "Static pages" do subject { page } describe "Home page" do before { visit root_path } it { should have_content('Sample App') } it { should have_title(full_title('')) } it { should_not have_title('| Home') } end describe "Help page" do before { visit help_path } it { should have_content('Help') } it { should have_title(full_title('Help')) } end describe "About page" do before { visit about_path } it { should have_content('About') } it { should have_title(full_title('About Us')) } end describe "Contact page" do before { visit contact_path } it { should have_content('Contact') } it { should have_title(full_title('Contact')) } end end
高度な設定
今度試してみよう。