4

我有一个设置为: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"并且我已经Capybara.javascript_driver = :webkit在 spec_helper.rb 中定义了。

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

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 项目放在一起来测试它,但是试图让它工作是令人沮丧的

4

3 回答 3

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

在给出问题的测试中,在 describe 标头中添加一个 :js => true 标志,如下所示:

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

我也不能将带有 :js => true 的 describe 块嵌套在其他 describe 块中,但必须独立运行,我认为这与从 rack_test 切换到 webkit 进行测试有关。

于 2013-11-16T17:56:43.813 回答
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"
于 2013-10-04T06:28:49.603 回答
-1

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

于 2013-10-03T11:42:19.067 回答