Skip to content

Instantly share code, notes, and snippets.

@Leoxxid
Created October 31, 2018 20:01
Show Gist options
  • Save Leoxxid/647709eba6ae8f4f162e3069d5af0f67 to your computer and use it in GitHub Desktop.
Save Leoxxid/647709eba6ae8f4f162e3069d5af0f67 to your computer and use it in GitHub Desktop.
Rspec Configuration Example
# support/api_helpers.rb
module ApiHelpers
def json_body
JSON.parse(response.body)
end
def set_api_headers
request.headers['Accept'] = 'application/vnd.api+json;'
request.headers['X-Access-Token'] = @user.authentication_token
request.headers['X-User-Email'] = @user.email
end
end
RSpec.configure do |config|
config.include ApiHelpers, type: :controller
end
# support/databa_cleaner.rb
require 'database_cleaner'
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation, { except: %w[cities states] })
DatabaseCleaner.clean
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
end
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'support/factory_bot'
# devise
include Warden::Test::Helpers
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end
Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
RSpec.configure do |config|
# devise reset
config.after :each do
Warden.test_reset!
end
# driven system configs
config.before(:each, type: :system) do
driven_by :rack_test
end
config.before(:each, type: :system, js: true) do
driven_by :selenium
end
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment