【问题标题】:Testing ajax searching with rspec使用 rspec 测试 ajax 搜索
【发布时间】:2014-12-24 12:46:11
【问题描述】:

我有一个奇怪的问题。我的功能测试如下所示: 需要“rails_helper”

feature "Doctors" do
  let(:ordering_unit) { build(:ordering_unit, name: "Test ordering unit") }
  let!(:doctor)  { create(:doctor, fullname: "Doctor", specialization: "Specialization", ordering_unit: ordering_unit)}
  let!(:doctor2) { create(:doctor, fullname: "Test doctort", specialization: "Test specialization") }
  before { visit doctors_path }

feature "Searching" do

  scenario "Should find proper doctors", js: true do
    fill_in "search_form_query", with: "Test"
    expect(page).to have_text(doctor2.fullname)
    expect(page).to_not have_text(doctor1.fullname)
    save_and_open_page
  end
end

这个测试负责测试索引页面上的ajax搜索。但问题是,当我运行这个测试时,我的 Firefox 窗口正在打开,并且在索引页面上没有显示任何医生。但是当我删除“js:true”选项并使用“save_and_open_page”检查页面时,两个医生在索引页面上。有什么问题? 我的 rails_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'capybara'
require 'rspec/rails'
require 'capybara/rspec'
ActiveRecord::Migration.maintain_test_schema!
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|

  config.include FactoryGirl::Syntax::Methods
  config.include Capybara::DSL

  config.before(:suite) do
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.clean_with(:truncation)
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end
  config.infer_spec_type_from_file_location!
end

【问题讨论】:

    标签: ruby-on-rails selenium rspec capybara


    【解决方案1】:

    当您使用启用了 javascript 的驱动程序时,您会希望使用截断策略而不是事务。

    将你的 before(:suite) 配置替换为:

    # need to reset the database to a clean state before running the whole test
    config.before(:suite) do
      DatabaseCleaner.clean_with(:truncation)
    end
    
    # set the default strategy to transactions since we want to run standard
    # tests as fast as possible
    config.before(:each) do
      DatabaseCleaner.strategy = :transaction
    end
    
    # When we use the javascript-enabled driver (some Capybara feature tests),
    # we'll want to use the truncation strategy.
    config.before(:each, js: true) do
      DatabaseCleaner.strategy = :truncation
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-30
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多