Skip to content

Instantly share code, notes, and snippets.

@mallain
Created July 23, 2010 13:55
Show Gist options
  • Save mallain/487461 to your computer and use it in GitHub Desktop.
Save mallain/487461 to your computer and use it in GitHub Desktop.
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'
require 'factory_girl'
require 'shoulda'
require 'faker'
require "authlogic/test_case"
require 'i18n'
class ActiveSupport::TestCase
# Transactional fixtures accelerate your tests by wrapping each test method
# in a transaction that's rolled back on completion. This ensures that the
# test database remains unchanged so your fixtures don't have to be reloaded
# between every test method. Fewer database queries means faster tests.
#
# Read Mike Clark's excellent walkthrough at
# http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
#
# Every Active Record database supports transactions except MyISAM tables
# in MySQL. Turn off transactional fixtures in this case; however, if you
# don't care one way or the other, switching from MyISAM to InnoDB tables
# is recommended.
#
# The only drawback to using transactional fixtures is when you actually
# need to test transactions. Since your test is bracketed by a transaction,
# any transactions started in your code will be automatically rolled back.
self.use_transactional_fixtures = true
# Instantiated fixtures are slow, but give you @david where otherwise you
# would need people(:david). If you don't want to migrate your existing
# test cases which use the @david style and don't mind the speed hit (each
# instantiated fixtures translates to a database query per test method),
# then set this back to true.
self.use_instantiated_fixtures = false
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
#fixtures :all
end
# Redefine #t method of TranslationHelper
# With this hack, all missing translation will raise a exception
module ActionView
module Helpers
module TranslationHelper
def t(key, options = {})
options[:raise] = true
I18n.translate(scope_key_by_partial(key), options)
end
end
end
end
# Define active_authlogic into all setup
class ActionController::TestCase
setup :activate_authlogic
end
# Generate an action
# param action : define the "get" action (string)
# ex : should_get_action("index")
def should_get_action(action)
context "on GET to #{action}" do
setup do
get action
end
should_respond_with :success
should_render_with_layout
should_render_template action
should_not_set_the_flash
end
end
# Return available locales for testing into ControllerTest
def available_testing_locales
I18n.available_locales
end
# Instanciate stub objects
# param name : Describe the name of model (string)
# ex : instanciate_stub_object('feedback')
def instanciate_stub_object(name)
# Instanciate objects array
objects = []
# Instanciate first object
instance_variable_set("@#{name}".to_sym, Factory.stub(name.to_sym))
# Instanciate five objects
(1..5).each do |i|
instance_variable_set("@#{name}_#{i}".to_sym, Factory.stub(name.to_sym))
instance_variable_get("@#{name}_#{i}").id = rand(3000)
objects << instance_variable_get("@#{name}_#{i}")
end
# Find method return an instanciate object
name.classify.constantize.stubs(:find).returns(instance_variable_get("@#{name}"))
# Find method with "all" parameter will return an array of instanciate objects
name.classify.constantize.stubs(:find).with(:all, anything).returns(objects)
end
# Generate setup with parameter
# param lang : Define the default_locale (string)
# param instanciate : Define the list of objects we have to instanciate in this setup Array with Hash
# ex : generate_setup(:lang => 'fr')
# ex : generate_setup(:lang => 'en', :instanciate => ['feedback', 'claim_collection')
def generate_setup(params={:instanciate => []})
# Instanciate session
LdapConnect.any_instance.stubs(:online?).returns(true)
UserSession.create!(Factory(:user))
# Create at least one agency
Factory(:agency)
# Feedback is need everywhere
instanciate_stub_object('feedback')
# Instanciate every object included in params[:instanciate] variable
if params.has_key?(:instanciate)
params[:instanciate].each do |object|
instanciate_stub_object(object)
end
end
# Define setup locale
I18n.default_locale = params[:lang]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment