以下を使用して、第 10 章の演習 1 と 2 のテストを static_pages_spec.rb に記述しました。これらの他のテストに合格すると、次のエラーが発生しました。
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 を超えるマイクロポストを作成するとすぐに、line 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>