Last active
October 7, 2019 07:19
-
-
Save stevenharman/2321262 to your computer and use it in GitHub Desktop.
Sensible RSpec config for clean, and slightly faster, specs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENV["RAILS_ENV"] ||= 'test' | |
require File.expand_path("../../config/environment", __FILE__) | |
require 'rspec/rails' | |
require 'rspec/autorun' | |
require 'capybara/rspec' | |
require 'webmock/rspec' | |
require 'factory_girl' | |
require 'factory_girl_rails' | |
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} | |
RSpec.configure do |config| | |
config.use_transactional_fixtures = false | |
config.infer_base_class_for_anonymous_controllers = false | |
config.include JsonSpec::Helpers | |
config.include LoginHelper, type: :request | |
config.include ActivitySpecHelper, type: :request | |
config.include RoomSpecHelper, type: :request | |
config.include AutocompleteSpecHelper, type: :request | |
config.before :suite do | |
DatabaseCleaner.clean_with :truncation | |
end | |
config.before type: :model do | |
DatabaseCleaner.strategy = :transaction | |
DatabaseCleaner.start | |
end | |
# Request specs cannot use a transaction because Capybara runs in a | |
# separate thread with a different database connection. | |
config.before type: :request do | |
DatabaseCleaner.strategy = :truncation | |
DatabaseCleaner.start | |
end | |
# Reset so other non-request specs don't have to deal with slow truncation. | |
config.after type: :request do | |
DatabaseCleaner.strategy = :transaction | |
end | |
config.before do | |
WebMock.disable_net_connect!(:allow_localhost => true) | |
ActionMailer::Base.deliveries.clear | |
end | |
config.after do | |
Timecop.return | |
DatabaseCleaner.clean | |
end | |
end | |
Capybara.javascript_driver = :webkit | |
Capybara.default_wait_time = 5 | |
Capybara.ignore_hidden_elements = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENV["RAILS_ENV"] ||= 'test' | |
require File.expand_path("../../config/environment", __FILE__) | |
require 'rspec/rails' | |
require 'rspec/autorun' | |
require 'capybara/rspec' | |
require 'webmock/rspec' | |
require 'factory_girl' | |
require 'factory_girl_rails' | |
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} | |
RSpec.configure do |config| | |
config.use_transactional_fixtures = false | |
config.infer_base_class_for_anonymous_controllers = false | |
config.include JsonSpec::Helpers | |
config.include LoginHelper, type: :request | |
config.include ActivitySpecHelper, type: :request | |
config.include RoomSpecHelper, type: :request | |
config.include AutocompleteSpecHelper, type: :request | |
config.before :suite do | |
DatabaseCleaner.strategy = :transaction | |
DatabaseCleaner.clean_with(:truncation) | |
end | |
# Request specs cannot use a transaction because Capybara runs in a | |
# separate thread with a different database connection. | |
config.before type: :request do | |
DatabaseCleaner.strategy = :truncation | |
end | |
# Reset so other non-request specs don't have to deal with slow truncation. | |
config.after type: :request do | |
DatabaseCleaner.strategy = :transaction | |
end | |
config.before do | |
DatabaseCleaner.start | |
WebMock.disable_net_connect!(:allow_localhost => true) | |
ActionMailer::Base.deliveries.clear | |
end | |
config.after do | |
DatabaseCleaner.clean | |
end | |
Capybara.javascript_driver = :webkit | |
Capybara.ignore_hidden_elements = false | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.use_transactional_fixtures = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.before :suite do | |
DatabaseCleaner.strategy = :transaction | |
DatabaseCleaner.clean_with(:truncation) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.before type: :request do | |
DatabaseCleaner.strategy = :truncation | |
end | |
config.after type: :request do | |
DatabaseCleaner.strategy = :transaction | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.before do | |
DatabaseCleaner.start | |
WebMock.disable_net_connect!(:allow_localhost => true) | |
ActionMailer::Base.deliveries.clear | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.after do | |
DatabaseCleaner.clean | |
end |
can I add the desired request headers here for example i want to add request.env['HTTP_ACCEPT'] = 'application/vnd.site+json; version=1'
Just came across this today and it helped me figure out my own setup. In case it helps you can check it out here: https://gist.github.com/mockdeep/9904695
The one major catch with these is that order matters, and after
hooks run in reverse order, so I use prepend_before
and append_after
to avoid confusion.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I updated this Gist to also reset the strategy after each
request
spec.