我有一個表格設置爲: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