You cannot set your test this way because RSpec is DSL. RSpec first reads the sample specification files before running the tests. Thus, he gets into Blog.all before running any of the tests. It also means that there is no database. Therefore, if there is no remaining state in the previous test run, Blog.all will return [] .
Trying to create objects in before will not work with how the test is written in your question. Again, this is because Blog.all runs during parsing, and before runs during testing.
To achieve what you want, you will probably have to break the βonly one testβ rule and embed Blog.all in the it block:
it "list the first five posts" do Blog.all(limit:5).each do |blog| expect(page).to have_selector('ul.accordmobile li#blog ul li a', text: blog.title, href: blog_path(blog.id)) end end
source share