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