Skip to content

Instantly share code, notes, and snippets.

@PhilippePerret
Created January 5, 2012 09:30
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 PhilippePerret/1564414 to your computer and use it in GitHub Desktop.
Save PhilippePerret/1564414 to your computer and use it in GitHub Desktop.
Spec helper (Rails 3.1)
# Inclure ce fichier dans tous les tests avec :
# require 'spec_helper'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Pour écrire les "it" dans test.log
config.before do |it|
Rails.logger.debug "\n***testcase***\n#{it.example.full_description}"
begin
Rails.logger.debug it.example.location
rescue
#ne rien faire
end
end
# Pour database_cleaner
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
# Pour utiliser t('...') de localisation dans les tests
def t texte, options = nil
I18n.t texte, options
end
def l temps, options = nil
I18n.l temps, options
end
# Pour utiliser `log "Message d'erreur"` dans les tests
def log texte
Rails.logger.debug texte
end
# ... Ici toutes les méthodes courantes utiles ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment