I am struggling with a simple test
require "rails_helper"
RSpec.feature "Home page", :type => :feature do
scenario "When a user visit it" do
visit "/"
expect(page).to have_css('article', count: 10)
end
end
in the view I have this code
<% @articles.each do |article| %>
<article>
<header> <%= article.title%> </header>
<div class="content-summary"> <%= article.body %> </div>
</article>
<hr/>
<% end %>
when I run the test I get
Failure/Error: expect(page).to have_css('article', count: 10)
expected to find css "article" 10 times but there were no matches
I run the server and I can see that 10 article tags exist.
when I change the view to this
<% 10.times do %>
<article>
<header> </header>
<div class="content-summary"> </div>
</article>
<hr/>
<% end %>
the test pass
Please help me
Rails have separate databases for each of the environment. Local server is run in development
mode by default and, therefore, is connected to the app_name_development
database. RSpec, however, run tests in test
environment and uses app_name_test
database. That is why articles you see when you run a server are not available in the RSpec test.
Tests require manual data setup. If you are using RSpec then, I assume, you have FactoryGirl installed as well. In this case test should probably look like this:
require "rails_helper"
RSpec.feature "Home page", :type => :feature do
let!(:articles) { create_list(:article, 10) }
scenario "When a user visit it" do
visit "/"
expect(page).to have_css('article', count: 10)
end
end