Devise를 Rspec으로 테스트해 보았습니다.

12816 단어 RSpecRailsdevise

병아리를 clone



히나가타는 여기 저장소를 사용합니다.
htps : // 기주 b. 코 m / s 치즈 622 / 라 ls_p 레세 t

설정 내용은 여기를 참조하십시오.
htps : // 이 m / ゔ _622 / ms / c b86733 562b32

Devise 설치



Gem 파일에 추가


gem 'devise'

bundle install


$ bundle

generator 실행


$ rails generate devise:install
Running via Spring preloader in process 21345
      create  config/initializers/devise.rb
      create  config/locales/devise.en.yml
===============================================================================

Some setup you must do manually if you haven't yet:

  1. Ensure you have defined default url options in your environments files. Here
     is an example of default_url_options appropriate for a development environment
     in config/environments/development.rb:

       config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

     In production, :host should be set to the actual host of your application.

  2. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:

       root to: "home#index"

  3. Ensure you have flash messages in app/views/layouts/application.html.erb.
     For example:

       <p class="notice"><%= notice %></p>
       <p class="alert"><%= alert %></p>

  4. You can copy Devise views (for customization) to your app by running:

       rails g devise:views

===============================================================================

development.rb 편집


config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

application.html.slim 편집


doctype html
html
  head
    title
      | RailsPreset
    = csrf_meta_tags
    = csp_meta_tag
    = stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload'
    = javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
  body
    = notice
    = alert
    = yield

일본어화



아래의 기사를 참고

User 모델 만들기



$ rails generate devise User
Running via Spring preloader in process 21493
      invoke  active_record
      create    db/migrate/20181106072923_devise_create_users.rb
      create    app/models/user.rb
      insert    app/models/user.rb
       route  devise_for :users
$ bin/rails db:migrate RAILS_ENV=development

신규 등록 테스트





도우미 만들기


spec/support/helpers.rb
require 'support/helpers/session_helpers'

RSpec.configure do |config|
  config.include Features::SessionHelpers, type: :feature
end
spec/support/session_helpers.rb
module Features

  module SessionHelpers

    def sign_up_with(email, password, confirmation)
      visit new_user_registration_path
      fill_in "Email", with: email
      fill_in "Password", with: password
      fill_in "Password confirmation", with: confirmation
      click_button "Sign up"
    end

    def sign_in(email, password)
      visit new_user_session_path
      fill_in "Email", with: email
      fill_in "Password", with: password
      click_button "Log in"
    end
  end
end

Feature 스펙 작성


spec/features/sign_up_spec.rb
require 'rails_helper'
 RSpec.feature "Sign Up", :devise do
  scenario "visitor can sign up with valid email and password" do
    sign_up_with("[email protected]", "password", "password")

    txts = [I18n.t("devise.registrations.signed_up"),
            I18n.t("devise.registrations.signed_up_but_unconfirmed")]
    expect(page).to have_content(/.*#{txts[0]}.*|.*#{txts[1]}.*/)
  end

  scenario "visitor cannot sign up with invalid email address" do
    sign_up_with("email", "password", "password")

    expect(page).to have_content("Emailは不正な値です")
  end

  scenario "visitor cannot sign up without password" do
    sign_up_with("[email protected]", "", "")

    expect(page).to have_content("Passwordを入力してください")
  end

  scenario "visitor cannot sign up with a short password" do
    sign_up_with("[email protected]", "1234", "1234")

    expect(page).to have_content("Passwordは6文字以上で入力してください")
  end

  scenario "visitor cannot sign up without password confirmation" do
    sign_up_with("[email protected]", "password", "")

    expect(page).to have_content("Password confirmationとPasswordの入力が一致しません")
  end

  scenario "visitor cannot sign up with mismatched password and confirmation" do
    sign_up_with("[email protected]", "password", "mismatch")

    expect(page).to have_content("Password confirmationとPasswordの入力が一致しません")
  end
end


확인


$ rspec spec/features/sign_up_spec.rb 
......

Finished in 0.461 seconds (files took 1.13 seconds to load)
6 examples, 0 failures

좋은 웹페이지 즐겨찾기