Capybara, Javascript, and RSpec integration tests with: remote => true

I have a form that is set to :remote => truewith two submit buttons (one for "Test Connection", one for creating / updating). My controller handles this correctly and displays the correct view based on the button clicked.

I have the following integration test to make sure that if the data source can connect, it will show the correct message to the user:

describe "Data Source Validation", :js => true do  

  before (:each) do
    @user = create_logged_in_user
  end

  it "returns true when data source is valid" do    
    DataSource.any_instance.stub(:can_connect).and_return(true)    
    visit new_data_source_path
    fill_in "Name", :with => "Example 123"
    fill_in "Host", :with => "myip.example.com"
    select "SQL Server", :from => "Database type"
    fill_in "Database name", :with => "Example"
    fill_in "Username", :with => "user"
    fill_in "Password", :with => "password"
    click_button "Test Connection"
    expect(page).to have_content "Successfully connected to database"
  end

end

I use gem "capybara-webkit"and I defined Capybara.javascript_driver = :webkitin spec_helper.rb.

When the test passes, I get the following result:

Failure/Error: expect(page).to have_content "Successfully connected to database"
    expected to find text "Successfully connected to database" in ...

When I view it in Chrome, it works exactly as I expect, with the correct error message.

How can I pass this test condition?

data_source_controller.rb, " "

begin      
  if @data_source.valid? && @data_source.can_connect?          
    format.js {render "valid_connection" }          
  else
    format.js {render "invalid_connection" }          
  end
rescue Exception => e
  format.js {render "invalid_connection", locals: {error_msg: e.message} }          
end

# 1
javascript : . wait_for_ajax :

 Failure/Error: wait_for_ajax
 Capybara::Webkit::InvalidResponseError:
   Javascript failed to execute

webkit /:

Failure/Error: expect(page).to have_content "Successfully connected to database"
   expected to find text "Successfully connected to database" in "Dashboard Reports Data Sources Account Create a New Data Source * Name * Host Port * Database type * Database name * Username * Password We encrypt all information in the database. Nothing can be retrieved without the proper credentials and encryption key. Copyright 2013"

, " " "" " "

, , github, ,

+4
3

rspec/capybara, ajax, . , :

GemFile

gem 'capybara-webkit'

127.0.0.1  testhost.com

spec_helper.rb

DEFAULT_HOST = "testhost.com"
DEFAULT_PORT = 7171  

RSpec.configure do |config|
  config.include Capybara::DSL  
  Capybara.javascript_driver = :webkit

  Capybara.default_host = "http://#{DEFAULT_HOST}"
  Capybara.server_port = DEFAULT_PORT
  Capybara.app_host = "http://#{DEFAULT_HOST}:#{Capybara.server_port}"

  #fixes issues with capybara not detecting db changes made during tests
  config.use_transactional_fixtures = false

  config.before :each do
    if Capybara.current_driver == :rack_test
      DatabaseCleaner.strategy = :transaction
    else
      DatabaseCleaner.strategy = :truncation
    end
    DatabaseCleaner.start
  end

  config.after do
    DatabaseCleaner.clean
  end  
end

a: js = > true :

 describe 'my test', :js => true do

: js = > true, , , , , rack_test webkit .

+4

:

def wait_for_ajax
  counter = 0
  while page.execute_script("return $.active").to_i > 0
    counter += 1
    sleep(0.1)
    raise "AJAX request took longer than 5 seconds." if counter >= 50
  end
end

:

click_button "Test Connection"
wait_for_ajax
expect(page).to have_content "Successfully connected to database"
0

, chrome, , this

-1

All Articles