Rspec + Capybara + Rails4 + Spork (intergation tests setup with selenium and poltergeist)

Posted Over 10 years ago. Visible to the public. Repeats.

Add in Gemfile:

group :development, :test do
  gem 'rspec-rails'
  gem 'spork-rails'
end

group :test do
  gem 'capybara'
  gem 'database_cleaner'
  gem 'email_spec'
  gem 'poltergeist'
  gem 'launchy'
  gem 'selenium-webdriver'
end

Run bundle install

Ensure your spec_helper.rb looks similar with this:

require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
# require 'spork/ext/ruby-debug'

Spork.prefork do
  # Loading more in this block will cause your tests to run faster. However,
  # if you change any configuration or code from libraries loaded here, you'll
  # need to restart spork for it take effect.

  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'email_spec'
  require 'rspec/autorun'

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    config.include EmailSpec::Helpers
    config.include EmailSpec::Matchers
    config.include Features::Helpers, type: :feature


    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
    config.fixture_path = "#{::Rails.root}/spec/fixtures"

    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, remove the following line or assign false
    # instead of true.
    config.use_transactional_fixtures = true

    # If true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
    config.infer_base_class_for_anonymous_controllers = true

    # Run specs in random order to surface order dependencies. If you find an
    # order dependency and want to debug it, you can fix the order by providing
    # the seed, which is printed after each run.
    #     --seed 1234
    config.order = "random"

    # DatabaseCleaner.clean_with(:truncation)
    DatabaseCleaner.strategy = :transaction

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

    config.before(:each) do
    end

    config.after(:all) do
      DatabaseCleaner.clean
    end

  end
end

Spork.each_run do
  # This code will be run each time you run your specs.
end

Then create folder spec/support and create here file capybara.rb, with content:
require "capybara/rails"
require 'capybara/rspec'
require 'capybara/poltergeist'

# Setup capybara webkit as the driver for javascript-enabled tests.
# Capybara.javascript_driver = :webkit


Capybara.default_selector = :css

Capybara.configure do |config|
  config.match = :prefer_exact
  config.ignore_hidden_elements = false
end


Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new app, window_size: [1600, 1200]
end

Capybara.register_driver :selenium_firefox do |app|
  Capybara::Selenium::Driver.new(app, :browser => :firefox)
end

Capybara.default_driver = :poltergeist
Capybara.current_driver = :poltergeist
Capybara.javascript_driver = :poltergeist

# In our setup, for some reason the browsers capybara was driving were
# not openning the right host:port. Below, we force the correct
# host:port.
Capybara.server_port = 7787

# We have more than one controller inheriting from
# ActionController::Base, and, in our app, ApplicationController redefines
# the default_url_options method, so we need to redefine the method for
# the two classes.
[ApplicationController, ActionController::Base].each do |klass|
  klass.class_eval do
    def default_url_options(options = {})
      { :host => "127.0.0.1", :port => Capybara.server_port }.merge(options)
    end
  end
end

If you decide to use Poltergeist, then you must install Phantomjs. Installation steps

Rspec integration tests must be placed in folder spec/features

That's all!

konjoot
Last edit
About 6 years ago
konjoot
Keywords
'Integration, tests', setup
Posted by konjoot to wiki (2013-12-20 06:56)