diff --git a/config/environments/test.rb b/config/environments/test.rb index 8a0fc8c6c99..7a6f94612a2 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -50,6 +50,10 @@ Discourse::Application.configure do config.log_level = :fatal end + if defined? RspecErrorTracker + config.middleware.insert_after ActionDispatch::Flash, RspecErrorTracker + end + config.after_initialize do SiteSetting.defaults.tap do |s| s.set_regardless_of_locale(:s3_upload_bucket, 'bucket') diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index d2b575f2944..fd977925cd5 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -14,6 +14,31 @@ require 'mocha/api' require 'certified' require 'webmock/rspec' +class RspecErrorTracker + + def self.last_exception=(ex) + @ex = ex + end + + def self.last_exception + @ex + end + + def initialize(app, config = {}) + @app = app + end + + def call(env) + begin + @app.call(env) + rescue => e + RspecErrorTracker.last_exception = e + raise e + end + ensure + end +end + ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' @@ -96,6 +121,18 @@ RSpec.configure do |config| end end + config.after :each do |x| + if x.exception && ex = RspecErrorTracker.last_exception + # magic in a cause if we have none + unless x.exception.cause + class << x.exception + attr_accessor :cause + end + x.exception.cause = ex + end + end + end + config.before :each do |x| # TODO not sure about this, we could use a mock redis implementation here: # this gives us really clean "flush" semantics, howere the side-effect is that @@ -126,6 +163,8 @@ RSpec.configure do |config| I18n.locale = :en + RspecErrorTracker.last_exception = nil + if $test_cleanup_callbacks $test_cleanup_callbacks.reverse_each(&:call) $test_cleanup_callbacks = nil