3

在阅读 Michael Hartl 写的 Learn Rails 书时,我被其中一个练习难住了。 通过示例学习 Rails,Michael Hartl

“为微博分页添加测试”

我的错误测试,放置在“描述“登录用户”中,如下所示:

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,测试都显示为通过。

任何“提示/帮助”将不胜感激

4

2 回答 2

5

在浏览一些存储库时,我找到了问题的答案——在创建额外的微帖子后,我需要再次访问 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
于 2012-12-11T17:27:46.603 回答
0

我认为你应该放一个过滤器来清理微柱的大量插入;就您的实现而言(除非您在此处未显示的测试代码的另一部分中执行此操作),它不会删除创建的微帖子。

这可以通过以下代码轻松完成:

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
于 2013-09-29T09:43:09.247 回答