我被卡住了,无法弄清楚我哪里出错了。我发现了类似的主题,但没有解决这些错误:
1)用户页面删除链接作为管理员用户管理员访问索引页面
Failure/Error: it { should have_link('delete', href: user_path(User.first)) }
expected link "delete" to return something
# ./spec/requests/user_pages_spec.rb:127:in `block (5 levels) in <top (required)>'
2)用户页面删除链接作为管理员用户管理员访问索引页面应该能够删除另一个用户
Failure/Error: expect { click_link('delete') }.to change(User, :count).by(-1)
Capybara::ElementNotFound:
no link with title, id or text 'delete' found
# (eval):2:in `click_link'
# ./spec/requests/user_pages_spec.rb:129:in `block (6 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:129:in `block (5 levels) in <top (required)>'
测试代码
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 { click_link('delete') }.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end
控制器:
def
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_url
end
指数:
<% provide(:title, 'All users') %>
<h1>All users</h1>
<%= will_paginate %>
<ul class="users">
<%= render @users %>
</ul>
<%= will_paginate %>
部分的
<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
<% if current_user.admin? && !current_user?(user) %>
| <%= link_to "delete", user, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</li>
任何指导/帮助将不胜感激......