我正在关注 Railstutorial(第 9 章)“删除链接”的测试第一次通过,但从下一次开始出现某种错误,例如:
Failures:
1) User pages index delete links as an admin user
Failure/Error: sign_in admin
Capybara::ElementNotFound:
Unable to find field "Email"
# ./spec/support/utilities.rb:20:in `sign_in'
# ./spec/requests/user_pages_spec.rb:38:in `block (5 levels) in <top (required)>'
2) User pages index delete links as an admin user
Failure/Error: sign_in admin
Capybara::ElementNotFound:
Unable to find field "Email"
# ./spec/support/utilities.rb:20:in `sign_in'
# ./spec/requests/user_pages_spec.rb:38:in `block (5 levels) in <top (required)>'
3) User pages index delete links as an admin user should be able to delete another user
Failure/Error: sign_in admin
Capybara::ElementNotFound:
Unable to find field "Email"
# ./spec/support/utilities.rb:20:in `sign_in'
# ./spec/requests/user_pages_spec.rb:38:in `block (5 levels) in <top (required)>'
Finished in 0.81336 seconds
4 examples, 3 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:48 # User pages index delete links as an admin user
rspec ./spec/requests/user_pages_spec.rb:42 # User pages index delete links as an admin user
rspec ./spec/requests/user_pages_spec.rb:43 # User pages index delete links as an admin user should be able to delete another user
Randomized with seed 11884
这是 utilities.rb文件的内容
include ApplicationHelper
def full_title(page_title)
base_title = "Welcome to Family & Friends"
if page_title.empty?
base_title
else
" #{base_title} | #{page_title} "
end
end
def sign_in(user, options={})
if options[:no_capybara]
# Sign in when not using Capybara.
remember_token = User.new_remember_token
cookies[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
else
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
end
错误指示的 user_pages_spec.rb 文件的一部分
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect do
click_link('delete', match: :first)
end.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end
end
session_helper.rb 文件,其中存在 sign_in 的定义
module SessionsHelper
def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(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
remember_token = User.encrypt(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end
def current_user?(user)
user == current_user
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 if request.get?
end
end
我什至尝试过类似帖子中建议的 fill_in "email_id" 但在这种情况下不起作用
谢谢!!
这是 html 为登录而呈现的 new.html.erb 的内容
<% provide(:title, "Sign in") %>
<h2>Sign in</h2>
<div class="row">
<div class="span6 offset3">
<%= form_for(:session, url: sessions_path) do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
<% end %>
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>