我有一个表格设置为:remote => true
与2个提交按钮(一个“测试连接”,一个创建/更新)。我的控制器正确处理这个问题,并根据点击的按钮呈现正确的视图。水豚,Javascript和RSpec集成测试与:远程=>真
我有以下的集成测试,以确保如果数据源可以连接,它会显示正确的信息给用户:
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 Capybara.javascript_driver = :webkit
。 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司机:硒和遇到同样的问题。我也尝试添加了“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项目来测试这个,但这是frus trating试图让这个工作
您确定这是一个AJAX问题,当AJAX请求未决时,have_content应该等待内容出现。您是否尝试过在click_button“Test Connection”之后添加睡眠并在运行规范时手动检查浏览器窗口中的内容?你看到控制器命中log/test.log? – Novae
您是否尝试过使用':selenium'驱动程序运行它? –
如果你在运行这个测试之后查看'log/test.log'(并且在你运行它之前删除这个文件,这个文件中唯一的东西就是这个测试的运行),你是否看到与你在开发中看到的相同的请求这是什么时候在Chrome中运行?看起来您的失败消息已被切断,我希望在失败消息中看到某些内容而不是“...”,这可能会提示页面上的内容。 – carols10cents