简体   繁体   中英

rails rspec capybara cannot get my internal api to connect

Constructing a basic rails app I'm re-factoring to do heavy lifting on an external docker/compute as a service ie iron.io. the 'worker'

In refactoring created Grape API to allow status of processing from remote 'worker' to notify the server when processing is done. The user interface then uses ajax to poll the local server to update. API and basic tests all ok. It also works in development using Delayed::job running the worker.

I however cannot seem to get my capybara tests to work end to end as the delayed::job running process making the HTTP request back to the server always gets connection refused.

It works fine if i run a rails server in parallel as the tests: ( RAILS_ENV="test" rails s -p 3001 ), then make sure the ENV variable is set to port 3001.

I had tried

  • various combination of Capybara.configure (as below)
  • in the test: visit url (where url="http://#{Capybara.server_host}:#{Capybara.server_port}" ) to see if that 'kicks off' the server perhaps
  • various webdrivers (poltergeist, selenium etc)

Any thoughts, experience or guidance much appreciated

Ben

note: in the code

  • populate the domain & port via ENV[''] variables that are populated (these environment variables will be set in the running environment iron.io)
  • port & app_host set as below
  • ENV variables populated in the test

     Capybara.configure do |config| config.run_server = true config.server_port = "9876" config.app_host = "http://127.0.0.1:9876" end 

    rails 4.1.0 rspec 3.4.0 capybara 2.7.0 poltergeist 1.5.1 selenium 2.53.0

I think you're trying to have your test too too much . I would recommend that you "mock out" the interactions with the other service to make the tests self sufficient. In the past I have added a test.js that:

  • Mocks out ajax on the page
  • Checks for specific requests to have been made ( page.evaluate_script )
  • Responds back to them in the way your external service will ( execute_script )

Like this:

# test.js
$.ajax = function(settings) {
    window.__ajaxRequests || (window.__ajaxRequests = []);
    window.__ajaxRequests.push(settings);

    return {
        done: function(cb) { settings.__done = cb; }
    }
}

# spec/features/jobs_spec.rb
visit '/jobs'
click_button 'Start job'

requests = page.evaulate_script('window.__ajaxRequests')
expect(requests.size).to eq(1)
expect(requests[0].url).to eq('http://jobs.yourproduct.com/start')
...

expect(page).not_to have_content('Job completed')
page.execute_script('window.__ajaxRequests[0].__done({data:{status:"complete"}})')

expect(page).to have_content('Job completed')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM