Skip to content

Instantly share code, notes, and snippets.

@kares
Created July 19, 2010 11:00
Show Gist options
  • Save kares/481277 to your computer and use it in GitHub Desktop.
Save kares/481277 to your computer and use it in GitHub Desktop.
example of using the disable_test_fixtures rails plugin
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'
# A plain old Rails test helper.
class ActiveSupport::TestCase
self.use_transactional_fixtures = true
self.use_instantiated_fixtures = false
# Setup all fixtures in test/fixtures/*.(yml|csv) for all
# tests in alphabetical order.
fixtures :all
# The disable_test_fixtures plugin provides this module
# to be included in test cases. No fixture changes happen
# yet by including it, we're only doing it here thus we
# don't have to include it in each and every test.
# We'll have two class methods available :
# #disable_fixtures_for and #enable_fixtures_for
include DisableTestFixtures
end
require 'test_helper'
class UserTest < ActiveSupport::TestCase
# disable fixtures for all test (names) not matching
# "remember_token" in this test case :
disable_fixtures_for { |test| test !~ /remember_token/ }
# an old test using fixtures
def test_should_unset_remember_token
steve = users(:steve)
steve.remember_me
assert_not_nil steve.remember_token
steve.forget_me
assert_nil steve.remember_token
end
# another old test using fixtures
def test_should_set_remember_token_for_one_week
steve = users(:steve)
before = 1.week.from_now
steve.remember_me_for 1.week
after = 1.week.from_now
assert_not_nil steve.remember_token
assert_not_nil steve.remember_token_expires_at
assert steve.remember_token_expires_at.between?(before, after)
end
# a new test that does not need fixtures
test 'should validate email on valid?' do
user = Factory.create_user
user.email = 'invalid@email'
assert ! user.valid?
assert user.errors.on(:email)
end
# another new test that does not rely on fixtures,
# esp useful if fixture data is "disabled" during
# this test as if there is an existing user with
# Steve's mail address it might clash !
test 'should not allow to change email' do
user = Factory.create_user :email => '[email protected]'
user.email = '[email protected]'
user.save!
user.reload
assert_equal '[email protected]', user.email
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment