DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Alexey has posted 28 posts at DZone. View Full User Profile

Acceptance Testing For Rails

03.25.2011
| 5875 views |
  • submit to reddit
        Make sure you have following gems in your Gemfile:

  gem "rspec", "2.0.1"
  gem "rspec-rails", "2.0.1"
  gem "capybara", ">= 0.4.1.1"
  gem "database_cleaner", ">= 0.6.6" 

Add spec/support/acceptance_example_group.rb:
require 'action_dispatch'
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara/dsl'

module AcceptanceExampleGroup
  extend ActiveSupport::Concern

  include RSpec::Rails::RequestExampleGroup
  include Rack::Test::Methods

  included do
    metadata[:type] = :acceptance
  end
end

RSpec.configure do |config|
  config.include AcceptanceExampleGroup,
     :example_group => { :file_path => /\bspec\/acceptance\// }

  config.use_transactional_fixtures = false

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

  config.before(:each) do
    if example.metadata[:js]
      Capybara.current_driver = :selenium
      DatabaseCleaner.strategy = :truncation
    else
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.start
    end
  end

  config.after(:each) do
    Capybara.use_default_driver if example.metadata[:js]
    DatabaseCleaner.clean
  end
end 

Delete line from spec/spec_helper.rb
  config.use_transactional_fixtures = true

Create spec/acceptance/some_acceptance_spec.rb
require 'spec_helper'

describe "some feature", :js => true do

  it "..." do
   visit "/"
  end
end

No you may create your acceptance tests.

===============================

Consider stake gem as alternative