我正在尝试在capybara进行简单的登录示例测试,您可以使用用户的电子邮件和密码登录,然后通过“登录”通知将其重定向到root_url
由于某种原因,capybara在我使用click_on登录后报告我在login_path“/ login”,但是当我运行它时我会在root_path“/”发出通知。
我在水豚或我的测试应用中错过了什么? 所有相关代码都应在下面。
控制器/ sessions_controller.rb
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_url, notice: "logged in"
else
redirect_to login_path, notice: "Email or password incorrect"
end
end
查看/会话/ new.html.erb
<h1>Sessions Log In</h1>
<%= form_tag sessions_path do %>
<div class="field">
<%= label_tag :email %><br />
<%= text_field_tag :email, params[:email] %>
</div>
<div class="field">
<%= label_tag :password %><br />
<%= password_field_tag :password %>
</div>
<div class="actions"><%= submit_tag "log in" %> </div>
<% end %>
login_spec.rb
it "should let you login with correct password" do
user = Factory.build(:user)
visit login_path
fill_in "Email", :with => user.email
fill_in "Password", :with => user.password
click_on "log in"
current_path.should == root_path
page.should have_content("logged in")
end
的routes.rb
...
get 'login', to: 'sessions#new', as: 'login'
...
Factories.rb
Factory.define :user do |f|
f.sequence(:email) {|n| "a#{}@a.a"}
f.password "a"
end