2

我有以下特征黄瓜测试使用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可能仍然干扰我的测试,还是我没有想到的其他内容?

任何想法都会非常受欢迎!

回答

0

我不认为它与数据库清理有关。这将不会清理,直到您的方案结束。要进行调试,我强烈建议您安装Capybara Screenshot,以便您可以准确查看页面中发生了什么。它为您提供screenshot_and_open_image的方法,您可以使用该方法弹出打开浏览器在此时的外观图像。我的猜测是:

  1. 由于计时问题,你的jQuery没有做你期望的 - 你试过更长的暂停吗?
  2. 您的交易未提交。你有没有在test.log中查看事务是否被提交?
  3. 您的表单未通过验证?尝试在I should be on the show battery page步骤的开头处输入screenshot_and_open_image以查看。我用另一件事是这样一个共享的一步,这样我可以在我的情况下加截图调试:

And /^I take a screenshot$/ do 
    Capybara::Screenshot.screenshot_and_open_image 
end