当我翻阅Michael Hartl写的“学习Rails”这本书时,我被其中一个练习难住了。Learn Rails by Example by Michael Hartl
“为micropost分页添加测试”
我的错误测试,放在'describe‘for signed in users’do‘中,如下所示:
describe "pagination" do
before(:all) do
30.times { FactoryGirl.create(:micropost, user: user) }
end
after(:all) { user.feed.delete_all }
page.should have_selector('div.pagination') }
it "should list each micropost" do
user.feed.paginate(page: 1).each do |user|
page.should have_selector('li', text: user.name)
end
end
end
无论我执行的是page.should还是page.should_not,测试都显示为通过。
任何“提示/帮助”都将不胜感激。
发布于 2012-12-12 01:27:47
在浏览一些回购时,我找到了问题的答案--在创建了额外的微帖子之后,我需要再次访问root_path。
describe "pagination" do
it "should paginate the feed" do
30.times { FactoryGirl.create(:micropost, user: user, content: "Consectetur adipiscing elit") }
visit root_path
page.should have_selector("div.pagination")
end
end
发布于 2013-09-29 17:43:09
我认为您应该放置一个毕竟过滤器来清理大量插入的微帖子;由于您的实现(除非您在测试代码的另一部分中执行,否则此处未显示),它不会删除创建的微帖子。
这可以通过以下代码轻松完成:
describe "pagination" do
after(:all) { user.microposts.delete_all unless user.microposts.nil? }
it "should paginate the feed" do
40.times { FactoryGirl.create(:micropost, user: user) }
visit root_path
page.should have_selector('div.pagination')
end
end
https://stackoverflow.com/questions/13810066
复制相似问题