Last active
April 28, 2016 09:37
-
-
Save jnicklas/090d7099afd6cb6349c5245e39d1e664 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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