Mateusz Urbański
Mateusz Urbański

Reputation: 7872

Testing ajax searching with rspec

I have strange problem. I have feature test that looks like this: require "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

This test is responsible for testing ajax search on index page. But the problem is that when I run the this test my firefox windows is opening and it not show any doctors on index page. But when I remove "js: true" option and check page using "save_and_open_page" two doctors are on index page. What can be wrong? My 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

Upvotes: 0

Views: 258

Answers (1)

TwiceB
TwiceB

Reputation: 959

When you use the javascript-enabled driver, you'll want to use the truncation strategy instead of transaction.

Replace your before(:suite) configuration with:

# 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

Upvotes: 1

Related Questions