Capybara,Javascript和RSpec集成测试:remote =>真正

时间:2013-09-29 21:12:43

标签: ruby-on-rails rspec capybara integration-testing

我有一个设置为:remote => true的表单,其中包含2个提交按钮(一个用于“测试连接”,一个用于创建/更新)。我的控制器正确处理此问题,并根据单击的按钮呈现正确的视图。

我进行了以下集成测试,以确保如果数据源可以连接,它将向用户显示正确的消息:

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

我正在使用gem "capybara-webkit",我在spec_helper.rb中定义了Capybara.javascript_driver = :webkit

测试运行时,我得到以下结果:

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

当我在Chrome中查看它时,它的工作方式正如我所期望的那样,并带有正确的错误消息。

如何让这个测试条件通过?

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驱动程序切换到:selenium并遇到了同样的问题。我还尝试添加“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项目放在一起进行测试,但是这样做会让人感到沮丧

3 个答案:

答案 0 :(得分:4)

我有一个类似的问题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

在给出问题的测试中添加:js =>描述标题中的true标志如下:

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

我也没有描述块:js => true嵌套在其他描述块中,但必须独立才能工作,我认为这与从rack_test切换到webkit进行测试有关。

答案 1 :(得分:0)

尝试创建辅助方法:

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"

答案 2 :(得分:-1)

我认为这可能是Chrome版本的问题,请尝试更新驱动程序,您甚至可以参考this