我被卡住了,找不到哪里出错了。我发现了类似的主题,但没有解决这些错误的方法:
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)用户页面删除链接作为admin用户admin访问索引页面应该能够删除其他用户
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>
任何指导/帮助都将不胜感激……
发布于 2013-09-07 05:50:52
也许我晚了一年才来,但我只是说要从同一教程(Rails tutorial from Michael Hartl)中学习Ruby on Rails,并面对同样的问题。
这里我发现了什么,我该如何修复(感谢我们的同事commets,我浏览了页面源代码并找到了问题所在。):
在教程中,Listing 9.43说:
需要“spec_helper”
描述“用户页面”做什么
主题{ page }
描述"index“do
让(:user ){ FactoryGirl.create(:user) }在用户访问分页之前结束它{ sign_in have_title('All users') }它{应该have_content('All users') }描述“分页”。。。end describe "delete links“do它{ should_not have_link(' delete ') } describe "as admin”do let(: admin ) { FactoryGirl.create(:admin) } do do sign_in admin able users_path end it {users_path have_link('delete',href: user_path(User.first)) }它“应该能够删除另一个用户”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。。。结束
检查与“删除链接”相关的部分是否在“分页”部分之后。分页说明从Listing 9.33开始,代码如下:
需要“spec_helper”
描述“用户页面”做什么
主题{ page }
describe "index“do let(: user ) { FactoryGirl.create(:user) }之前(:each) do sign_in用户访问users_path结束
it {应该列出(‘all users') } it {应该列出所有用户(’all users') }描述“分页”之前(:have_title){30次{ FactoryGirl.create(:user) }}之后(:all){ User.delete_all } it {应该have_selector('div.pagination') } it“应该列出每个用户”do User.paginate(.each: 1).each do |user| expect(page).to have_selector('li',文本: user.name)结束。。。结束
我意识到after(:all) { User.delete_all }
语句擦除了测试数据库中该部分之后的所有记录(让我继续,因为这是如此明显:P )。当RSpec测试继续“删除链接”时,会创建任何用户。
下一步是创建一个新的'admin‘用户,它是测试数据库中目前唯一的用户。正如预期的那样:
删除它{应该删除(‘
’,href: user_path(User.first)) }
将失败,因为当前用户不能删除自己,并且它没有任何“删除”链接。同样,下一个测试也会失败:
click_link('delete',match::first)
解决方案(解决方法或w/e :D ):
我相信有一些方法可以解决这个问题,我想分成两个(第一个是我使用的那个)
答:我添加了一个非admin用户,在admin用户尝试登录之前,这让我在test DB中有2个用户,代码如下:
describe "delete links“do它{ should_not have_link('delete') }
describe "as admin user“do let(: admin ) { FactoryGirl.create(:admin) } do do FactoryGirl.create(:user,name:"test",email:"test@example.com") sign_in admin访问users_path end it { have_link('delete',href: user_path(User.first)) }它“应该能够删除另一个用户”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
语句FactoryGirl.create(:user, name: "test", email: "test@example.com")
完成了这项工作。
B.将所有“删除链接”部分移到“分页”部分之前,因为我猜在这一点上,至少存在在*user_pages_spec.rb*.开头创建的第一个用户
就是这样,有了这个,我可以获得绿色的“删除链接”的新功能。
我的两点意见。
iVieL。
发布于 2014-03-05 05:28:04
第一个固定的测试就在那里,忽略其余的。如果你的解决方案看起来像下面的,那么你就是金子。将整个块移动到分页内容之上的第二个想法也适用。
describe 'delete links' do
it { should_not have_link('delete') }
describe 'as an admin user' do
let(:admin) { FactoryGirl.create(:admin) }
before do
FactoryGirl.create (:user)
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
结束
https://stackoverflow.com/questions/13058872
复制相似问题