Skip to content

Instantly share code, notes, and snippets.

@chrisbloom7
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbloom7/1bcee9b33254d134be42 to your computer and use it in GitHub Desktop.
Save chrisbloom7/1bcee9b33254d134be42 to your computer and use it in GitHub Desktop.
TestUnit setup/teardown order in Ruby 2.1 with shoulda-context, factory_girl_rails and database_cleaner
$ bin/rake test:integration TEST=test/integration/my_test.rb
Starting test run...
Starting DatabaseCleaner...
Linting factories...
Post lint cleaning up with DatabaseCleaner...
Loaded suite /Users/chrisbloom7/.rvm/rubies/ruby-2.1.4/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader
Started
Running test setup...
Running test A...
Global teardown clean up with DatabaseCleaner...
Running test teardown...
.Running test setup...
Running context setup...
Running context B setup...
Running test C inside context B...
Running context B teardown...
Running context teardown...
Global teardown clean up with DatabaseCleaner...
Running test teardown...
.Running test setup...
Running context setup...
Running test B inside context B...
Running context teardown...
Global teardown clean up with DatabaseCleaner...
Running test teardown...
.
Finished in 7.984922 seconds.
source 'https://rubygems.org'
ruby '2.1.4'
gem 'rails', '~> 3.2.0'
gem 'mysql2'
group :development, :test do
gem 'byebug'
end
group :development do
gem 'spring'
gem 'spring-commands-testunit'
end
group :test do
gem 'database_cleaner', '~> 1.4'
gem 'factory_girl_rails', '~> 4.5'
gem 'shoulda', '~> 3.5'
end
require File.dirname(__FILE__) + '/../test_helper'
class MyTest < ActionController::IntegrationTest
setup do
puts "Running test setup..."
end
teardown do
puts "Running test teardown..."
end
should "test A" do
puts "Running test A..."
end
context "context A" do
setup do
puts "Running context setup..."
end
teardown do
puts "Running context teardown..."
end
should "test B" do
puts "Running test B inside context B..."
end
context "context B" do
setup do
puts "Running context B setup..."
end
teardown do
puts "Running context B teardown..."
end
should "test C" do
puts "Running test C inside context B..."
end
end
end
end
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'database_cleaner'
# Require ruby files in support dir.
Dir[File.expand_path('test/support/*.rb')].each { |file| require file }
begin
puts "Starting test run..."
puts "Starting DatabaseCleaner..."
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.start
# Test for valid factories
puts "Linting factories..."
FactoryGirl.lint
ensure
puts "Post lint cleaning up with DatabaseCleaner..."
DatabaseCleaner.clean
end
class Test::Unit::TestCase
include FactoryGirl::Syntax::Methods
def teardown
puts "Global teardown clean up with DatabaseCleaner..."
DatabaseCleaner.clean
end
end
class ActiveSupport::TestCase
self.use_transactional_fixtures = true
self.use_instantiated_fixtures = false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment