Michael Michael - 6 months ago 9
Ruby Question

Capybara integration test object creation

I have a basic integration test that is using Capybara, the problem is that if I do not create the required objects firstly the integration test fails. Am I required to create all objects as the first step in an integration test using Capybara? I am using

Rails 4.2.4
with
Capybara 2.4.3


Fail

scenario 'if media content contains more than 10 items display pagination links' do
sign_in
# Object creation
11.times do
FactoryGirl.create(:media_content)
end
within '.pagination' do
expect(page).to have_content '1'
end
end


Success

scenario 'if media content contains more than 10 items display pagination links' do
# Object creation
11.times do
FactoryGirl.create(:media_content)
end
sign_in
within '.pagination' do
expect(page).to have_content '1'
end
end

Answer

If the objects creation affects the page that you are visit-ing in your capybara test then yes, you need to create the objects before you test for elements on that page because upon visiting the page, its content is already grabbed by the test browser.

I assume that you have a visit "some_login_page" and perhaps a redirect upon successful login in your sign_in method, so when finishing the sign_in, the test browser already visited (i.e. grabbed) the page on which you are trying to test content later.

The only exception that comes to my mind is if you used a delayed AJAX request to grab the newly created elements from the server to the page dynamically - in that case creating the objects after page visit might work ok.

Comments