When doing integration tests, I usually use factory_girl to quickly set up the required objects for my test. However, when I use js: true and poltergiest, capybara runs the web browser in a separate thread, so the objects may not be created when it starts clicking through the web pages. Here is an example
require 'spec_helper'
feature 'Bike administration' do
scenario 'create a bike', js: true do
user = create(:admin)
dealer = create(:dealer)
brand = create(:brand, name: "Trek")
# Since the browser is on a separate thread, this can potentially run
# before the user is finished being created
sign_in_as(user)
visit admin_root_path
click_link "Dealers"
view_bikes_for(dealer)
click_on "New Bike"
fill_out_bike_form "Superfly 100"
add_bike_photo
save
...
end
end
I ended up creating the following helper module that I would call like wait_until{dealer.persisted?}
right after my factory girl functions and before I start navigating on the website.
#Found here: https://gist.github.com/jnicklas/d8da686061f0a59ffdf7
module WaitHelper
def wait_until
require "timeout"
Timeout.timeout(Capybara.default_wait_time) do
sleep(0.1) until value = yield
value
end
end
end
RSpec.configure do |config|
config.include WaitHelper, type: :feature
end
Is this how you handle setting up your javascript integration tests? Do you create objects from factories and then use some sort of waiting function before you start navigating the site?