Here is my partial code:
<% @birds.each do |bird| %>
<p>
<%= "#{bird.name} , #{bird.genus} #{bird.species}"%>
</p>
<% end %>
And here is my RSpec code that tests the webpage:
let(:bird) { FactoryGirl.create(:bird) }
before { visit birds_path }
it { should have_text("#{bird.name}") }
This test fails:
Failure/Error: it { should have_text("#{bird.name}") }
expected #has_text?("Bird_1") to return true, got false
Does anyone know why that happens and how to fix this? Thanks
The let is called the first time you try to access bird. Since you already visited the page via the before block, the bird was not yet created.
Please try the following:
it "should have bird" do
bird = FactoryGirl.create(:bird)
visit bird_path
expect(page).to have_content(bird.name)
end
The problem apparently was that FactoryGirl wasn't finished creating the object, so the object didn't show up on the page. Instead of
let(:bird) { FactoryGirl.create(:bird) }
it should have been
let!(:bird) { FactoryGirl.create(:bird) }
Thanks for the suggestions though.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.