ruby - Rails: Delete request in capybara, bug or my mistake? -
in michael hartl's rails tutorial (rails 3.2), in listing 9.52:
describe "when signing in again" before delete signout_path print page.html <---- insert here visit signin_path print page.html <---- insert here again fill_in "email", with: user.email fill_in "password", with: user.password click_button "sign in" end "should render default (profile) page" page.should have_selector('title', text: user.name) end end
i inserted 2 prints. and, surprisingly got printout of same page (which shouldn't be, supposed bring root url after sending delete
request). after happens, since visit signin_path
takes me sign in page, sign in procedure succeeds , test case. however, second print page.html
gave me header of user whom still signed in.
when changed delete signout_path
click_link "sign out"
, worked.
did miss in code or it's capybara bug? (cause i'm pretty sure followed right..)
update: if change delete signout_path
capybara.current_session.driver.delete signout_path
works fine. (meaning capybara signs out user correctly)
update
these files (sessions controller , helper):
sessions_controller.rb
class sessionscontroller < applicationcontroller def new end def create user = user.find_by_email(params[:session][:email].downcase) if user && user.authenticate(params[:session][:password]) sign_in user redirect_back_or user else flash.now[:error] = 'invalid email/password combination' render 'new' end end def destroy sign_out redirect_to root_url end end
sessions_helper.rb
module sessionshelper def sign_in(user) cookies.permanent[:remember_token] = user.remember_token self.current_user = user end def signed_in? !current_user.nil? end def current_user=(user) @current_user = user end def current_user?(user) user == current_user end def signed_in_user unless signed_in? store_location redirect_to signin_url, notice: "please sign in." end end def current_user @current_user ||= user.find_by_remember_token(cookies[:remember_token]) end def sign_out self.current_user = nil cookies.delete(:remember_token) end def redirect_back_or(default) redirect_to(session[:return_to] || default) session.delete(:return_to) end def store_location session[:return_to] = request.url end end
routes.rb
demoapp::application.routes.draw ... root to: 'static_pages#home' match '/signup', to: 'users#new' match '/signin', to: 'sessions#new' match '/signout', to: 'sessions#destroy', via: :delete resources :sessions, only: [:new, :create, :destroy] ... end
delete signout_path
not work on capybara feature specs. get
, post
, put
, delete
controller spec specific methods , not available in feature specs.
you want making actual requests (via click, submit, etc...) in capybara specs since trying test.
if want ensure session clear "setup" it's best via model.
as aside, if @ describing "when signing in again", want "log out" can "sign in again".
Comments
Post a Comment