I have a page that’s rendering lots of stuff when I really use it, but renders nothing in Capybara.
Controller goes like this:
class IdeasController < ApplicationController
def index
@ideas = Idea.all
end
Relevant bit of the page goes like this:
<div class="accordion" id="accordion">
Ideas length is <%= @ideas.length %>.
<% @ideas.each do |idea| %>
<div class="accordion-group">
<div class="accordion-heading">
<%= link_to(idea.title, '#idea_' + idea.id.to_s, class: "accordion-toggle", data: {toggle: "collapse", parent: "#accordion"} ) %>
</div>
<%= div_for(idea, class: "accordion-body collapse") do %>
<div class="accordion-inner">
<%= render :partial => "idea", locals: { :idea => idea } %>
</div>
<% end %>
</div>
<% end %>
</div>
When viewed in Development, this works beautifully and I get loads of stuff in the view (my db is full of mock data). But when I pop it open mid-test using save_and_open_page, it’s completely empty, and says “Ideas length is 0”.
I did rake db:test:clone and rake db:test:prepare, but nada — stays empty.
Can you help me figure out why? Is there a way to peek into the test db and see if anything’s there?
Edit: Inspired by the comments, I realized the trouble must be that the test environment flushes things out every time, so obviously there’s nothing. In an (apparently misguided) attempt to resolve this, I did:
before do
create(:idea)
visit root_path
click_link "Log In"
end
Note the create(:idea) line, which was supposed to trigger my Idea factory and stick a mock idea into the db (the factory works fine with other tests).
Now the page says:
Internal Server Error
SQLite3::BusyException: database is locked: INSERT INTO “users” (“created_at”, “email”, “name”, “provider”, “uid”, “updated_at”) VALUES (?, ?, ?, ?, ?, ?)
which is beyond weird. Rails s is running in the background.
As requested by commenter, full spec follows:
require 'spec_helper'
describe "Idea page", js: true do
subject { page }
before do
create(:idea)
visit root_path
click_link "Log In"
end
context "Single idea" do
before do
save_and_open_page
#click_link('Eligendi sint quod quia alias sed sit vitae repellendus.')
end
it { should have_selector('a',text:'Claim') }
end
end
Creating the data using a factory is the correct approach. To fix the sqlite error, you might need to increase the timeout for the database.
To do this, in
config/database.ymlFind the
test:environment and settimeout:To a larger value. As recommended by the question SQLite3::BusyException
After that, ensure that in
spec/spec_helper.rbthat you have the following line:And that you are using something like database_cleaner to keep your test db clean between tests.