james james - 1 month ago 21
Ajax Question

End of file Rspec error with AJAX

I'm trying to test that I can

POST
a survey via AJAX

Test code:

describe "when completing the survey" do
before do
visit "/orders/#{@order.disguised_id}?key=#{ENV['admin_code']}"
find("#show-survey").click
find(".modal#survey-box select[name='heard']").find("[value='Other']").select_option
find("#submit-survey").click
end

it "should submit correctly" do
@order.reload
@order.heard.should == "Other"
end
# test post survey
end


First off, I can confirm via manual testing that this works. The issue is that the spec fails:

1) when completing the survey should submit correctly
Failure/Error: find("#submit-survey").click
EOFError:
end of file reached
# ./spec/features/edit_order/editing_order_spec.rb:<<the line with find("#submit-suvery")>>


When it fails, the Rspec auto ends the run and I'm left with an open browser window (using Selenium) and a regular command line interface. This is the controller that receives the
POST


def survey
@order = Order.find_by_disguised_id(order_params[:disguised_id])
@order.update_attributes(order_params)
render nothing: true
end

Answer

If you're using chrome with selenium make sure you're running the latest version of chromedriver (2.24 as of this answer).

Additionally - click is asynchronous so reloading the order immediately after clicking #submit-survey is going to be flaky at best since it's highly likely it will be reloading @order before the ajax request is processed. You need to wait for whatever visual change on the page would indicate the survey has been submitted after clicking.

find("#submit-survey").click
page.should have_text('something') # whatever shows up when the survey finishes submitting - or use have_css, etc

Also the previous line find(".modal#survey-box select[name='heard']").find("[value='Other']").select_option can be written in a more readable manner as

find(".modal#survey-box").select('Other', from: 'heard')
Comments