ã¦ã¼ã¶ã¼èªè¨¼ã®ãã¹ãï¼2ï¼ -- ã¢ã¯ã·ã§ã³ã®ä»®å®è£
2013/09/01
ååã«å¼ãç¶ãããã¹ãé§åéçºï¼TDDï¼ã®æ¹å¼ã§ãã°ã¤ã³ã»ãã°ã¢ã¦ãæ©è½ã®å®è£ ãç¶ãã¾ãã
ãã¹ãã³ã¼ãã®ãããã
ã¦ã¼ã¶ã¼èªè¨¼æ©è½ã®ãã¹ã spec/features/login_and_logout_spec.rb
ã¯ç¾å¨ãã®ãããªå½¢ããã¦ãã¾ãã
require 'spec_helper' describe 'ãã°ã¤ã³' do specify 'ã¦ã¼ã¶ã¼èªè¨¼æå' do visit root_path within('form#new_session') do fill_in 'username', with: 'taro' fill_in 'password', with: 'correct_password' click_button 'ãã°ã¤ã³' end expect(page).not_to have_css('form#new_session') end specify 'ã¦ã¼ã¶ã¼èªè¨¼å¤±æ' do visit root_path within('form#new_session') do fill_in 'username', with: 'taro' fill_in 'password', with: 'wrong_password' click_button 'ãã°ã¤ã³' end expect(page).to have_css('p.alert', text: 'ã¦ã¼ã¶ã¼åã¾ãã¯ãã¹ã¯ã¼ããæ£ããããã¾ããã') expect(page).to have_css('form#new_session') end end
ãã¾ãã¹ããè½ã¡ã¦ããã®ã¯ã9è¡ç®ã¨19è¡ç®ã§ããããã°ã¤ã³ããã¿ã³ãã¯ãªãã¯ããã¨ãã©ã¼ã ãã¼ã¿ã Rails ã¢ããªã±ã¼ã·ã§ã³ã«å¯¾ãã¦éä¿¡ãããã®ã§ãããã¾ã éä¿¡å ã®ã¢ã¯ã·ã§ã³ãåå¨ããªãã®ã§ãããã§ã¨ã©ã¼ãçºçãã¾ãã
ã«ã¼ãã£ã³ã°ãããã¦ã¢ã¯ã·ã§ã³ã®ä»®å®è£
ã¾ããã¦ã¼ã¶ã¼èªè¨¼ãè¡ãã¢ã¯ã·ã§ã³ãURLãHTTP ã¡ã½ããã®ç¨®é¡ã決ãã¾ããããç¹ã«æ±ºã¾ãã¯ããã¾ããã®ã§ãèªè
ã®çããã®æ
£ç¿ã«å¾ã£ã¦ãã ãããããã§ã¯ãã¢ã¯ã·ã§ã³ã sessions#create
ã¨ããPOST
ã¡ã½ãã㧠/login
ã«ã¢ã¯ã»ã¹ãããã¨ã«ãã¾ãã
次ã«ããã®æ±ºå®äºé ãã¢ããªã±ã¼ã·ã§ã³æ¬ä½ã®ã½ã¼ã¹ã³ã¼ãã«æ¸ãè¾¼ãã§ããã¾ãã
config/routes.rb
ãä¿®æ£ï¼
Sinope::Application.routes.draw do post 'login' => 'sessions#create', as: :login root to: 'top#index' end
2è¡ç®æ«å°¾ã® as: :login
ã¯ã/login
ã¨ãã URL ãã¹ã :login
ã¨ããååã§åç
§ã§ããããã«ããããã®ãªãã·ã§ã³ã§ãã
app/controllers/sessions_controller.rb
ãæ°è¦ä½æï¼
class SessionsController < ApplicationController def create redirect_to :root end end
ã¾ã ã¢ã¯ã·ã§ã³ã®ä¸èº«ã¯å®è£
ãã¾ããããã ãã空ã®ã¾ã¾ã«ãã¦ãã㨠sessions/create
ã¨ãããã³ãã¬ã¼ããåå¨ããªãã¨ããä¾å¤ãçºçãã¦åã«é²ã¾ãªãã®ã§ãã¨ããããããããã¼ã¸ã«ãªãã¤ã¬ã¯ã·ã§ã³ãã¦ããã¾ãã
app/views/top/_login_form.html.erb
ãä¿®æ£ï¼
<%= form_tag :login, id: 'new_session' do %> <div> <%= label_tag 'username', 'ã¦ã¼ã¶ã¼å' %> <%= text_field_tag 'username' %> </div> <div> <%= label_tag 'password', 'ãã¹ã¯ã¼ã' %> <%= password_field_tag 'password' %> </div> <div> <%= submit_tag 'ãã°ã¤ã³' %> </div> <% end %>
å¤æ´ç®æã¯1è¡ç®ã§ããform_tag
ã®ç¬¬1å¼æ°ã ''
ãã :login
ã«å¤æ´ãã¾ããã
ãã®ç¶æ ã§ãã¹ããå®è¡ããã¨ã次ã®ãããªçµæã«ãªãã¾ãï¼
Failures: 1) ãã°ã¤ã³ ã¦ã¼ã¶ã¼èªè¨¼æå Failure/Error: expect(page).not_to have_css('form#new_session') Capybara::ExpectationNotMet: expected not to find css "form#new_session", found 1 match: "ã¦ã¼ã¶ã¼å ãã¹ã¯ã¼ã" # ./spec/features/login_and_logout_spec.rb:11:in `block (2 levels) in <top (required)>' 2) ãã°ã¤ã³ ã¦ã¼ã¶ã¼èªè¨¼å¤±æ Failure/Error: expect(page).to have_css('p.alert', text: 'ã¦ã¼ã¶ã¼åã¾ãã¯ãã¹ã¯ã¼ããæ£ããããã¾ããã') Capybara::ExpectationNotMet: expected to find css "p.alert" with text "ã¦ã¼ã¶ã¼åã¾ãã¯ãã¹ã¯ã¼ããæ£ããããã¾ããã" but there were no matches # ./spec/features/login_and_logout_spec.rb:21:in `block (2 levels) in <top (required)>'
失æãå¼ãèµ·ããã¦ããç®æãã11è¡ç®ã¨21è¡ç®ã«é²ã¿ã¾ããã
ã¦ã¼ã¶ã¼èªè¨¼å¤±æã·ããªãªã®å®è£
ããããæ¬ä¸¸ã«è¿ã¥ãã¦ãã¾ããã
sessions#create
ã¢ã¯ã·ã§ã³ãå®è£
ãã¾ãããã
ã§ããããã¾ã§ã®ããã«ã¯åç´ã§ã¯ããã¾ãããã¦ã¼ã¶ã¼èªè¨¼ãæåããå ´åã¨å¤±æããå ´åã§ã·ããªãªãåå²ããããã§ãã
両æ¹åæã«å®è£ ãããã¨ããã®ã¯æ··ä¹±ã®å ã
ç°¡åãããªå¾è ã®æ¹ãããã£ã¤ãã¾ããããã
app/controllers/sessions_controller.rb
ãä¿®æ£ï¼
class SessionsController < ApplicationController def create if false # æªå®è£ else flash.alert = 'ã¦ã¼ã¶ã¼åã¾ãã¯ãã¹ã¯ã¼ããæ£ããããã¾ããã' end redirect_to :root end end
app/views/layouts/application.html.erb
ãä¿®æ£ï¼<%= render 'layouts/flashes' %>
ã追å ï¼ï¼
<!DOCTYPE html> <html> <head> <title>Sinope</title> <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= csrf_meta_tags %> </head> <body> <%= render 'layouts/flashes' %> <%= yield %> </body> </html>
app/views/layouts/_flashes.html.erb
ãæ°è¦ä½æï¼
<% if flash.alert.present? %> <p class="alert"><%= flash.alert %></p> <% end %> <% if flash.notice.present? %> <p class="notice"><%= flash.notice %></p> <% end %>
ããã§ãã¦ã¼ã¶ã¼èªè¨¼å¤±æãã®ã¨ã°ã¶ã³ãã«ãéãã¾ãããã¹ãã®å®è¡çµæã¯æ¬¡ã®éãï¼
Failures: 1) ãã°ã¤ã³ ã¦ã¼ã¶ã¼èªè¨¼æå Failure/Error: expect(page).not_to have_css('form#new_session') Capybara::ExpectationNotMet: expected not to find css "form#new_session", found 1 match: "ã¦ã¼ã¶ã¼å ãã¹ã¯ã¼ã" # ./spec/features/login_and_logout_spec.rb:11:in `block (2 levels) in <top (required)>'
ä»åã¯ããã¾ã§ã¨ãã¾ãããã
次åã¯
次åã®ãã¦ã¼ã¶ã¼èªè¨¼ã®ãã¹ã(3)ãã§ã¯ãã¤ãã« RSpec ã®è¯ããã¢ãã¯ããç»å ´ãã¾ããã§ã¯ãã¾ãã