Skip to content

Instantly share code, notes, and snippets.

@jnicklas
Last active April 28, 2016 09:37
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 jnicklas/090d7099afd6cb6349c5245e39d1e664 to your computer and use it in GitHub Desktop.
Save jnicklas/090d7099afd6cb6349c5245e39d1e664 to your computer and use it in GitHub Desktop.
# Prevents requests from hitting the Rails app after the test has finished.
class CapybaraRequestBlockerMiddleware
# assignment to initialized instance variables is thread safe in Ruby
@enabled = false
class << self
def enabled?
@enabled
end
def enable!
@enabled = true
end
def disable!
@enabled = false
end
end
def initialize(app)
@app = app
end
def call(env)
if self.class.enabled?
@app.call(env)
else
[200, {}, ["Request blocked by block middleware"]]
end
end
end
Capybara.app = CapybaraRequestBlockerMiddleware.new(Rails.application)
RSpec.configure do |config|
config.before do
CapybaraRequestBlockerMiddleware.enable!
end
config.after do
CapybaraRequestBlockerMiddleware.disable!
Capybara.reset_sessions!
DatabaseCleaner.clean
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment