【发布时间】:2012-04-15 02:08:47
【问题描述】:
我已经完成了 Ruby on Rails 教程的第 9 章,并添加了我自己的功能来在用户首次注册时锁定用户,这样管理员必须进入并批准(“解锁”)他们的 ID在新用户访问该站点之前。我添加了一个 :locked 布尔属性,其工作方式与 User 对象的 :admin 属性类似。我现在一切正常,但我无法为它编写一个简单的测试。我在 user_pages_spec.rb 中添加了以下测试,就在层次结构“分页”-“作为管理员用户”下:
describe "as an admin user to unlock new users" do
let(:admin) { FactoryGirl.create(:admin) }
let(:locked_user) { FactoryGirl.create(:locked) }
before do
sign_in admin
visit users_path
end
it { should have_link('unlock', href: user_path(locked_user)) }
it { should_not have_link('unlock', href: user_path(admin)) }
end
为了支持创建“锁定”用户,将其添加到 factory.rb:
factory :locked do
locked true
end
我可以通过 Firefox 手动确认解锁链接是否显示,但我仍然遇到以下故障:
1) User pages index pagination as an admin user to unlock new users
Failure/Error: it { should have_link('unlock', href: user_path(locked_user)) }
expected link "unlock" to return something
# ./spec/requests/user_pages_spec.rb:64:in `block (5 levels) in <top (required)>'
我很想知道 a) 为什么会失败 :),还有 b) 如何调试这样的问题。我如何判断测试实际上“看到”了什么?根据另一个 stackoverflow 用户的建议,我尝试使用 rails-pry 解决不同的问题,但在这种情况下,我发现它的用途有限。
任何想法或建议将不胜感激。提前致谢!
-马特
【问题讨论】:
标签: ruby-on-rails railstutorial.org