Skip to content

Instantly share code, notes, and snippets.

@wzdf1982
Created December 30, 2011 02:17
Show Gist options
  • Save wzdf1982/1537321 to your computer and use it in GitHub Desktop.
Save wzdf1982/1537321 to your computer and use it in GitHub Desktop.
Setting up a Rails Test Suite
# Step 7. Launch Guard and start writing tests
# Guard will automatically launch Spork and run RSpec
bundle exec guard
# Step 1. Update your "Gemfile"
gem 'haml'
gem 'haml-rails', :group => :development
group :assets do
gem 'twitter-bootstrap-rails'
gem 'coffee-rails'
gem 'compass-rails'
gem 'jquery-rails'
gem 'sass-rails'
gem 'therubyracer', '0.10.2', :platforms => :ruby
gem 'uglifier'
end
group :development do
gem 'thin'
gem 'quiet_assets'
end
group :development, :test do
gem 'rspec-rails'
gem 'rb-fsevent', '~> 0.9.1'
gem 'factory_girl_rails'
gem 'faker'
# Auto testing
gem 'guard-rspec'
gem 'guard-konacha'
gem 'guard-spork'
gem 'rb-inotify', :require => false
# Javascript
gem 'konacha'
gem 'chai-jquery-rails'
gem 'sinon-chai-rails'
gem 'sinon-rails'
gem 'ejs'
end
group :test do
gem "capybara"
gem 'database_cleaner'
gem 'shoulda-matchers'
gem 'spork'
gem 'mocha'
gem 'poltergeist',require: 'capybara/poltergeist'
end
# Step 2. Install and setup Gems
bundle install
rails g rspec:install
spork --bootstrap
mkdir spec/models spec/requests spec/support
echo -e "FactoryGirl.define do\nend" > spec/factories.rb
guard init spork
guard init rspec
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'shoulda-matchers'
Capybara.javascript_driver = :poltergeist
# 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.treat_symbols_as_metadata_keys_with_true_values = true
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :mocha
# 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 = false
# 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 = false
# Database Cleaner
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.include FactoryGirl::Syntax::Methods
end
end
Spork.each_run do
# This code will be run each time you run your specs.
FactoryGirl.reload
end
# Step 4. Create and update your "Guardfile"
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
watch('config/application.rb')
watch('config/environment.rb')
watch('config/environments/test.rb')
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile')
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
watch('test/test_helper.rb') { :test_unit }
watch(%r{features/support/}) { :cucumber }
end
guard 'rspec' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara features specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end
# Step 5. Update your ".rspec" file
--colour
--drb
--format nested
# Step 6. -Optional- configure your "config/application.rb" file
config.generators.stylesheets = false
config.generators.javascripts = false
config.generators.helper = false
config.generators.test_framework :rspec, fixtures: false, views: false, view_specs: false, routing_specs: false, helper_specs: false, controller_specs: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment