Rspec統合テスト編 ~ Railsチュートリアル ~

間違っている所や、良い書き方あればコメントくれると喜びます

全部を置き換えたら学ぶことがなく、時間をただ浪費することになるので

ログインを置き換えます。

コード

FactoryBot.define do
  factory :user do
    name { Faker::Name.name }
    sequence(:email) { |n| "rails_tutorial#{format('%03d', n)}@example.com" }
    password { "password" }
    password_confirmation { "password" }
    activated { true }
    activated_at { Time.zone.now }

    trait :user_with_microposts do
      transient do
        microposts_count { 5 }
      end

      after(:create) do |user, evaluator|
        create_list(:micropost, evaluator.microposts_count, user: user)
      end
    end
  end
end

  

[support/request_spec_helper.rb]
module RequestSpecHelper
  def is_logged_in?
    !session[:user_id].nil?
  end

    # テストユーザーとしてログインする
  def log_in_as(user)
    session[:user_id] = user.id
  end

  def log_in_as(user, password: 'password', remember_me: '1')
    post login_path, params: { session: { email: user.email,
                                          password: password,
                                          remember_me: remember_me } }
  end
end

  

[rails_helper.rb]
require 'rspec/rails'
require 'capybara/rails'
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

RSpec.configure do |config|
  config.include RequestSpecHelper, type: :request
end

  

[features/users_login_spec.rb]
require 'rails_helper'

RSpec.feature "UsersLogins", type: :feature do

  let(:user) { create(:user) }

  scenario "jump user show page" do
    visit login_path
    fill_in "Email", with: user.email
    fill_in "Password", with: user.password

    click_button "Log in"
    expect(page).to have_text(user.name)
    expect(page).to have_link('Settings', href: edit_user_path(user))
    expect(page).to have_link('Log out', href: logout_path)
  end

  scenario "email is blank" do
    visit login_path
    fill_in "Email", with: ""
    fill_in "Password", with: user.password
    click_button "Log in"
    expect(page).to_not have_text(user.name)
    expect(page).to have_link('Log in', href: login_path)
    expect(page).to have_text("Invalid email/password combination")
  end

  scenario "password is blank" do
    visit login_path
    fill_in "Email", with: user.email
    fill_in "Password", with: ""
    click_button "Log in"
    expect(page).to_not have_text(user.name)
    expect(page).to have_link('Log in', href: login_path)
    expect(page).to have_text("Invalid email/password combination")
  end

  scenario "email and password is blank" do
    visit login_path
    fill_in "Email", with: ""
    fill_in "Password", with: ""
    click_button "Log in"
    expect(page).to_not have_text(user.name)
    expect(page).to have_link('Log in', href: login_path)
    expect(page).to have_text("Invalid email/password combination")
  end
end

  

[requests/logins_spec.rb]
require 'rails_helper'

RSpec.describe "Logins", type: :request do

  let(:user) { create(:user) }

  scenario "login with valid information followed by logout" do
    get login_path
    post login_path, params: { session: { email: user.email, password: user.password } }

    expect(is_logged_in?).to be true
    expect(response).to redirect_to(user)
    follow_redirect!
    expect(response).to render_template('users/show')
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(user)
    delete logout_path
    expect(is_logged_in?).to be false
    expect(response).to redirect_to(root_url)
    delete logout_path
    follow_redirect!
    assert_select "a[href=?]", login_path
    assert_select "a[href=?]", logout_path,      count: 0
    assert_select "a[href=?]", user_path(user), count: 0
  end

  scenario "login with remembering" do
    log_in_as(user, remember_me: '1')
    expect(cookies['remember_token']).to_not be_empty
  end

  scenario "login without remembering" do
    # クッキーを保存してログイン
    log_in_as(user, remember_me: '1')
    delete logout_path
    # クッキーを削除してログイン
    log_in_as(user, remember_me: '0')
    expect(cookies['remember_token']).to be_empty
  end
end

テストの置き換えは公式ドキュメントを読めば、すんなりできたがsession[:user_id]をfeaturesで書くことが

許可されておらずsupportのhelperを書くことに気づくのに時間がかかった。

しかし、config.include RequestSpecHelper, type: :request

でincludeはclassが許可されず、module出なければならないなど学ぶことを多い改造となった。

参考文献

Publisher: RSpec - Relish

GitHub - teamcapybara/capybara: Acceptance test framework for web applications