ruby on rails - How to visit page with login user, rspec -
i'm beginer in ruby on rail , i'm trying use rspec check controller. have trouble visit page in spec. here user_spec.rb:
#!/bin/env ruby # encoding: utf-8 require 'user' require 'rails_helper' describe user "login sucess" visit "welcome/index" fill_in "session_user_id", :with => "1992111-1234" fill_in "session_password", :with => "sa1234" click_button "ログイン" end "main menu" visit "welcome/mainmenu" end end
when run spec, have error:
i can't pass through shop_no because dont have current_user: <%= current_user.shop_no%>
. so, must login user first, don't know how work rspec. tried creat login(user) in spec/user.rb:
#!/bin/env ruby # encoding: utf-8 require "rails_helper" def login(user) visit root_path fill_in "session_user_id", :with => "1992111-1234" fill_in "session_password", :with => "sa1234" click_button "ログイン" end
but don't know how work that. so, please me !
update login page:
<div class="medium-4 medium-centered row" style="padding: 5px"> <div class="medium-10 medium-centered columns"> <% if flash[:notice] %> <div class="notice"> <%= flash[:notice] %> </div> <% end %> <%= form_for(:session, url: login_path) |f| %> <%= f.label(:user_id, 'user id') %> <%= f.text_field :user_id %> <%= f.label :password %> <%= f.password_field :password %> <center> <%= f.submit "ログイン", class: "button [tiny large]", :style => "border-radius: 0px 0px" %> </center> <% end %> </div> </div>
there better ways should work. logs user in every test. might have add code log out user before next test.
change it "login success" do
line:
describe user before :each # <===== line visit "welcome/index" fill_in "user_id", :with => "1992111-1234" # <=== updated fill_in "password", :with => "sa1234" # <=== updated click_button "ログイン" end ... end
Comments
Post a Comment