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