2012-11-06 36 views
0

我在static_pages_spec.rb中編寫了第10章練習1和2的測試,當我得到其他測試通過時,我得到以下內容錯誤:Railstutorial(Michael Hartl)Chatper 10練習2:分頁斷開Feed測試

1) Static pages Home page for signed-in users should render the user's feed 
    Failure/Error: page.should have_selector("li##{item.id}", text: item.content) 
     expected css "li#1138" with text "Lorem ipsum" to return something 
    # ./spec/requests/static_pages_spec.rb:25:in `block (5 levels) in <top (required)>' 
    # ./spec/requests/static_pages_spec.rb:24:in `block (4 levels) in <top (required)>' 

很明顯,只要FactoryGirl創建超過30個微博,item.id測試就會以某種方式打破。

這裏是static_pages_spec.rb:

describe "Home page" do 
    before { visit root_path } 

    it { should have_selector('h1', text: 'Sample App') } 
    it { should have_selector('title', text: full_title('')) } 

    describe "for signed-in users" do 
     let(:user) { FactoryGirl.create(:user) } 
     before do 
     31.times { FactoryGirl.create(:micropost, user: user) } 
     sign_in user 
     visit root_path 
     end 

     after { user.microposts.delete_all } 

     it "should render the user's feed" do 
     user.feed.each do |item| 
      page.should have_selector("li##{item.id}", text: item.content) 
     end 
     end 

     it "should have micropost count and pluralize" do 
     page.should have_content('31 microposts') 
     end 

     it "should paginate after 31" do 
     page.should have_selector('div.pagination') 
     end 
    end 

    end 

這是我的_feed_item.html.erb部分:

<li id="<%= feed_item.id %>"> 
    <%= link_to gravatar_for(feed_item.user), feed_item.user %> 
    <span class="user"> 
    <%= link_to feed_item.user.name, feed_item.user %> 
    </span> 
    <span class="content"><%= feed_item.content %></span> 
    <span class="timestamp"> 
    Posted <%= time_ago_in_words(feed_item.created_at) %> ago. 
    </span> 
    <% if current_user?(feed_item.user) %> 
    <%= link_to "delete", feed_item, method: :delete, 
            data: { confirm: "You sure?" }, 
            title: feed_item.content %> 
    <% end %> 
</li> 
+0

我覺得它有什麼用它做是無法看到的下一個頁面。因爲每次都會返回非常高的訂單項ID。 –

回答

0

我通過限制進料管線項測試,以只檢查第一固定這28個職位。

it "should render the user's feed" do 
    user.feed[1..28].each do |item| 
     page.should have_selector("li##{item.id}", text: item.content) 
    end 
    end 

但是,我不知道這是解決問題的最佳方法。

2

我不知道它的相關性,但我會發布它,所以它可能會幫助其他人。

您的主頁只顯示30個源項目(因爲PAGINATE),但你的循環請檢查是否所有的飼料在您的主頁,他們不存在,這就是爲什麼你有錯誤...

我解決您的問題,這與你相似,使用分頁,而不是範圍

it "should render the user's feed" do 
    user.feed.paginate(page: 1).each do |item| 
    page.should have_selector("li##{item.id}", text: item.content) 
    end 
end 
相關問題