我有以下特徵黃瓜測試使用typeahead.js輸入形式:typeahead.js測試失敗與黃瓜和水豚-Webkit的導軌4
@javascript
Scenario: Creating a battery using typeahead
When I create a new battery using typeahead
Then I should be on the show battery page
And I should see the battery created message
上用下面的錯誤的第二步驟中的測試失敗消息:
ActiveRecord::RecordNotFound (ActiveRecord::RecordNotFound)
./features/step_definitions/admin/car_part_steps/battery_steps.rb:37:in `/^I should be on the show battery page$/'
features/admin/creating_car_parts/creating_batteries.feature:20:in `Then I should be on the show battery page'
相關步驟的定義如下:
When /^I create a new battery using typeahead$/ do
select_from_typeahead :field => 'battery_manufacturer_typeahead',
:select => @manufacturer.name
fill_in 'Type', :with => '700W'
click_button 'Create Battery'
end
Then /^I should be on the show battery page$/ do
battery = Battery.find_by_type_and_manufacturer_id!('700W', @manufacturer.id)
current_path.should == admin_battery_path(battery)
page.should have_content(battery.type)
end
的select_from_typeahead功能如下:
def select_from_typeahead(params)
params[:js_field] ||= params[:field]
params[:select_typeahead] ||= params[:select]
fill_in params[:field], :with => params[:select][0, 2]
page.execute_script "$('##{params[:js_field]}').trigger('focus')"
page.execute_script "$('##{params[:js_field]}').trigger('keydown')"
sleep 0.5
page.execute_script "$('.tt-suggestion:contains(\"#{params[:select_typeahead]}\")').trigger('mouseenter').trigger('click')"
end
問題不似乎有什麼,做的預輸入本身然而,由於代碼在瀏覽器中工作,如果我加入一些調試輸出,我注意到,在運行測試時,電池會在第一步中保存到數據庫中,在第二步運行之前它會神祕地消失。
我認爲這是一個與database_cleaner有關的問題,因爲我知道Javascript設置爲使用事務時不會很好地使用Javascript,但我已經嘗試將其設置爲使用截斷,並禁用事務性的裝置,工作。
我的功能/支持/ env.rb目前看起來是這樣的:
require 'simplecov'
SimpleCov.start 'rails'
require 'cucumber/rails'
Capybara.default_selector = :css
Capybara.javascript_driver = :webkit
ActionController::Base.allow_rescue = false
Cucumber::Rails::World.use_transactional_fixtures = false
DatabaseCleaner.strategy = :truncation
Cucumber::Rails::Database.javascript_strategy = :truncation
我的環境如下:
rails 4.0.2
cucumber 1.3.10
cucumber-rails 1.4.0
capybara 2.2.0
capybara-webkit 1.1.0
database_cleaner 1.2.0
我缺少的東西,有一些其他的方式database_cleaner可能仍然干擾我的測試,還是我沒有想到的其他內容?
任何想法都會非常受歡迎!