james james - 9 months ago 80
Ajax Question

End of file Rspec error with AJAX

I'm trying to test that I can

a survey via AJAX

Test code:

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

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

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
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

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

Answer Source

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.

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')