Skip to content

Instantly share code, notes, and snippets.

@knoopx
Forked from agibralter/exception_helper.rb
Created November 9, 2011 14:30
Show Gist options
  • Save knoopx/1351591 to your computer and use it in GitHub Desktop.
Save knoopx/1351591 to your computer and use it in GitHub Desktop.
Accessing app errors when using Capybara/Selenium with RSpec
# -*- encoding : utf-8 -*-
# Because Capybara.app gets run by Thin in a separate thread, errors raised in
# the application during request_specs get hidden and just show up in the
# selenium-controller browser as 500 errors with no extra info. This module
# helps us dig into those errors.
module RequestSpecs
module ExceptionHelper
@@last_exception = nil
def self.last_exception
@@last_exception
end
def self.last_exception=(e)
@@last_exception = e
end
class Middleware
def initialize(app)
@app = app
end
def call(env)
begin
@app.call(env)
rescue Exception => e
ExceptionHelper.last_exception = e
Rack::Response.new('error in app', 500).finish
end
end
end
end
end
RSpec.configure do |config|
config.before(:each, type: :request) do
Capybara.app = RequestSpecs::ExceptionHelper::Middleware.new(Capybara.app)
end
config.around(:each, type: :request) do |example|
example.run
app_exception = RequestSpecs::ExceptionHelper.last_exception
if !example.metadata[:allow_errors] && app_exception
RequestSpecs::ExceptionHelper.last_exception = nil
@example.instance_variable_set(:@exception, app_exception)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment