事前準備
gemのインストール
group:test do
gem 'capybara', '~> 2.15.2'
end
spec/rails_helper.rbにCapybaraの設定を追加
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require'capybara/rspec'
フィーチャーテストの実施
スペックファイルの作成
rails g rpsec:feature projects
テスト実装
require 'rails_helper'
RSpec.feature "Projects", type: :feature do
scenario "user create a new project" do
user = FactoryBot.create(:user)
visit root_path
click_link "Sign in"
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Log in"
expect do
click_link "New Project"
fill_in "Name", with: "Test Project"
fill_in "Description", with: "Trying out Capybara"
click_button "Create Project"
expect(page).to have_content "Project was successfully created."
expect(page).to have_content "Test Project"
expect(page).to have_content "Owner: #{user.name}"
end.to change(user.projects, :count).by(1)
end
end