【问题标题】:RubyTutorial 10.5.2 Rspec test micropost paginationRubyTutorial 10.5.2 Rspec 测试微博分页
【发布时间】:2012-12-10 22:04:09
【问题描述】:

在阅读 Michael Hartl 撰写的 Learn Rails 书籍时,我被其中一个练习难住了。 Learn Rails by Example by 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,测试都显示为通过。

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

【问题讨论】:

    标签: ruby-on-rails-3 rspec-rails railstutorial.org


    【解决方案1】:

    在浏览一些存储库时,我找到了问题的答案 - 在创建额外的微博之后,我需要再次访问 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
    

    【讨论】:

    • 这帮助我弄清楚了分页测试。
    【解决方案2】:

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

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

    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
    

    【讨论】:

      猜你喜欢
      • 2011-10-28
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多