Skip to content

Instantly share code, notes, and snippets.

@MatiasFernandez
Created May 3, 2016 17:25
Show Gist options
  • Save MatiasFernandez/8453d559c2e82596c9499677e7df8af8 to your computer and use it in GitHub Desktop.
Save MatiasFernandez/8453d559c2e82596c9499677e7df8af8 to your computer and use it in GitHub Desktop.
Log Capybara Exceptions
# Note: Add the uncommented lines inside the Application class definition inside application.rb file
# [...]
# class XYZ < Rails::Application
if Rails.env == 'test'
require 'diagnostic'
config.middleware.use(::Diagnostic)
end
# Note: Put this class in 'lib'
class Diagnostic
FILENAME = File.join(Rails.root, 'log', 'test.log')
def initialize(app)
@app = app
end
def call(env)
return @app.call(env)
rescue StandardError => e
trace = e.backtrace.select{ |l|l.start_with?(Rails.root.to_s) }.join("\n ")
msg = "#{e.class}\n#{e.message}\n#{trace}\n"
File.open(FILENAME, 'a') { |f|
f.write "\n"
f.write "\n"
f.write " --- BACKTRACE --- \n"
f.write "\n"
f.write msg
f.write "\n"
f.write " --- BACKTRACE --- \n"
f.write "\n"
f.write "\n"
}
raise e
end
end
# Note: Make sure this setting exist inside test.rb file
config.action_dispatch.show_exceptions = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment