LoginSignup
0
0

More than 1 year has passed since last update.

【RSpec】Railsチュートリアル第6版 第7章

Last updated at Posted at 2021-10-30

はじめに

Railsチュートリアル第6版のテストをRSpecで書き直していく。

目次

Minitest

ユーザー登録に対するテスト

test/integration/users_signup_test.rb
require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest

  test "invalid signup information" do
    get signup_path
    assert_no_difference 'User.count' do
      post users_path, params: { user: { name:  "",
                                         email: "user@invalid",
                                         password:              "foo",
                                         password_confirmation: "bar" } }
    end
    assert_template 'users/new'
  end

  test "valid signup information" do
    get signup_path
    assert_difference 'User.count', 1 do
      post users_path, params: { user: { name:  "Example User",
                                         email: "user@example.com",
                                         password:              "password",
                                         password_confirmation: "password" } }
    end
    follow_redirect!
    assert_template 'users/show'
  end
end

RSpec

ユーザー登録に対するテスト

spec/system/users_signup_spec.rb
require 'rails_helper'

RSpec.describe "UsersSignups", type: :system do

  it "invalid signup information" do
    visit root_path
    click_on "Sign up"
    expect {
      fill_in "Name", with: ""
      fill_in "Email", with: "user@invalid"
      fill_in "Password", with: "foo"
      fill_in "Confirmation", with: "bar"
      click_button "Create my account"
    }.to_not change(User, :count)
    expect(page).to have_current_path users_path
    expect(page).to have_content("Name can't be blank")
    expect(page).to have_content("Email is invalid")
    expect(page).to have_content("Password confirmation doesn't match Password")
    expect(page).to have_content("Password is too short (minimum is 6 characters)")
  end

  it "valid signup information" do
    visit root_path
    click_on "Sign up"
    expect {
      fill_in "Name", with: "Example User"
      fill_in "Email", with: "user@example.com"
      fill_in "Password", with: "password"
      fill_in "Confirmation", with: "password"
      click_button "Create my account"
    }.to change(User, :count).by(1)
    expect(page).to have_current_path user_path(User.last)
    expect(page).to have_content("Welcome to the Sample App!")
  end

end

click_onでユーザー登録ページに移動し、fill_inでフォームに情報を入力した後、click_buttonで情報を送信する。
expect(page).to have_current_pathで正しいページに移動しているかチェックし、expect(page).to have_contentでエラーメッセージとフラッシュがページに存在しているかチェック。
最後の2行は、後に、ユーザー新規登録後有効化メールを送信しホームページに戻るように仕様変更するので削除することになる。

0
0
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
0
0