Skip to content

Instantly share code, notes, and snippets.

@irrationalidiot
Forked from jacortinas/description
Created April 5, 2011 14:13
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save irrationalidiot/903664 to your computer and use it in GitHub Desktop.
Save irrationalidiot/903664 to your computer and use it in GitHub Desktop.
Cucumber, rspec and spork setup using watchr for rails
Gems:
gem 'cucumber'
gem 'cucumber-rails'
gem 'rspec'
gem 'rspec-rails'
gem 'capybara'
gem 'database_cleaner'
gem 'spork'
gem 'watchr'
Commands:
rspec:install
cucumber:install
spork --bootstrap
Modify these files:
.watchr
lib/tasks/watchr.rake
spec/spec_helper.rb
features/support/env.rb
config/environments/test.rb # config.cache_classes = true
Commands:
bundle exec spork cucumber
bundle exec spork rspec
rake watchr
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= "test"
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
require 'cucumber/formatter/unicode' # Remove this line if you don't want Cucumber Unicode support
require 'cucumber/rails/world'
require 'cucumber/rails/active_record'
require 'cucumber/web/tableish'
require 'capybara/rails'
require 'capybara/cucumber'
require 'capybara/session'
# Lets you click links with onclick javascript handlers without using @culerity or @javascript
# Commented out because it causes bugs :-\
#require 'cucumber/rails/capybara_javascript_emulation'
Capybara.default_selector = :css
end
Spork.each_run do
ActionController::Base.allow_rescue = false
Cucumber::Rails::World.use_transactional_fixtures = true
if defined?(ActiveRecord::Base)
begin
require 'database_cleaner'
DatabaseCleaner.strategy = :truncation
rescue LoadError => ignore_if_database_cleaner_not_present
end
end
end
# This usually sits at the root of the project and is called ".watchr"
@spec_cmd = "bundle exec rspec"
@cuc_cmd = "bundle exec cucumber"
# Convenience methods ########################################################
# Working on eventually adding Growl support. The problem right now is catching
# the output of a command, while at the same time piping it out to the stdout.
# Right now I don't know how to do it without stripping the ANSI color tags.
# def growl(message)
# if message.match /(\d+)\s(errors?|failures?)/ # Rspec failures
# Growl.notify "#{$1} spec #{$2}", :title => 'Watchr: specs failing'
# elsif message.match /(\d+)\sfailed/ # Cucumber failures
# Growl.notify "#{$1} features failed", :title => 'Watchr: features failing'
# end
# end
def run(command)
puts "\n\n"
puts command
system command
puts "\n\n"
@interrupted = false
end
def run_all_specs
result = run "#{@spec_cmd} spec/"
end
def run_spec(spec)
result = run "#{@spec_cmd} #{spec}"
end
def related_specs(path)
Dir['spec/**/*.rb'].select do |file|
file =~ /#{File.basename(path).split('.').first}_spec.rb/
end
end
def run_all_features
result = run @cuc_cmd
end
def run_feature(feature)
result = run "#{@cuc_cmd} #{feature}"
end
def run_suite
run_all_specs
run_all_features
end
# Watchr rules ###############################################################
watch('spec/spec_helper\.rb') { run_all_specs }
watch('spec/support/.*') { run_all_specs }
watch('spec/.*_spec\.rb') { |m| run_spec m[0] }
watch('app/.*\.rb') { |m| related_specs(m[0]).map { |s| run_spec s } }
watch('lib/.*\.rb') { |m| related_specs(m[0]).map { |s| run_spec s } }
watch('features/support/.*') { |m| run_all_features }
watch('features/.*\.feature') { |m| run_feature m[0] }
# Signals ####################################################################
@interrupted = false
Signal.trap 'QUIT' do # CTRL-\
puts " --- Running all specs ---\n\n"
run_all_specs
end
Signal.trap 'INT' do # CTRL-C
if @interrupted
abort "\n"
else
puts 'Interrupt a second time to quit'
puts " --- Running entire test suite ---\n\n"
@interrupted = true
sleep 1.5
run_suite
end
end
require 'spork'
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 'database_cleaner'
#require 'remarkable/active_record'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
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
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
desc "Run Watchr"
task :watchr do
sh %{bundle exec watchr .watchr}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment