Created
November 7, 2011 06:21
-
-
Save agibralter/1344306 to your computer and use it in GitHub Desktop.
Accessing app errors when using Capybara/Selenium with RSpec
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
# -*- 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