Skip to content

Instantly share code, notes, and snippets.

@levinalex
Created September 9, 2009 16:57
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 levinalex/183881 to your computer and use it in GitHub Desktop.
Save levinalex/183881 to your computer and use it in GitHub Desktop.
# demonstrates that fixtures are not cleaned after unit tests are run (or between different unit tests)
# setup a fresh rails project
rails foo
cd foo
# create a model
./script/generate model bar
rake db:migrate
# create a fixture and a test
cat <<EOF > test/fixtures/bars.yml
one:
updated_at:
two:
updated_at:
EOF
# create a test which loads the fixture
cat <<EOF > test/unit/bars_test.rb
require 'test_helper'
class BarTest < ActiveSupport::TestCase
fixtures :bars
test "fixtures should be loaded" do
assert_equal 2, Bar.count
end
end
EOF
# don't load fixtures in test_helper
cat <<EOF > test/test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'
class ActiveSupport::TestCase
self.use_transactional_fixtures = true
self.use_instantiated_fixtures = false
end
EOF
# migrate the db, run tests
rake db:migrate
rake test:units
# now check the test db, should be empty but isn't
./script/runner -e test "puts Bar.count" #=> prints '2', should print '0'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment