我有以下 Cucumber 功能使用 typeahead.js 测试输入表单:
@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 不兼容,但我已经尝试将其设置为使用截断并禁用事务装置,但它仍然不起作用。
我的 features/support/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 可能仍会干扰我的测试,或者它完全是我没有想到的其他东西?
任何想法都会非常受欢迎!