Skip to content

Instantly share code, notes, and snippets.

@arsduo
Created March 30, 2016 14:55
Show Gist options
  • Select an option

  • Save arsduo/733b802ef1d542c0e1227be59b519500 to your computer and use it in GitHub Desktop.

Select an option

Save arsduo/733b802ef1d542c0e1227be59b519500 to your computer and use it in GitHub Desktop.
Reproduction of RSpec issue with double used in raised errors

RSpec Issue with Doubles used in Raised Errors

If your code raises an error whose message attempts to reference a property of a test double, RSpec will throw an exception complaining that the lifecycle

Reproduction

bundle && rspec example_spec.rb will reproduce the issue.

Expected

A normal RSpec error message saying an exception was raised in that particular test, including the exception's message. Other tests render properly.

FF

Failures:

  1) MyError has a message including the object's body
     Failure/Error: raise MyError.new(object)

     MyError:
       I ran into a problem

     # ./example_spec.rb:19:in `block (2 levels) in <top (required)>'

  2) MyError still shows other failure messages
     Failure/Error: expect(1).to be(2)

       expected #<Fixnum:5> => 2
            got #<Fixnum:3> => 1

       Compared using equal?, which compares object identity,
       but expected and actual are not the same object. Use
       `expect(actual).to eq(expected)` if you don't care about
       object identity in this example.
     # ./example_spec.rb:24:in `block (2 levels) in <top (required)>'

Finished in 0.00796 seconds (files took 0.07125 seconds to load)
2 examples, 2 failures

Failed examples:

rspec ./example_spec.rb:14 # MyError has a message including the object's body
rspec ./example_spec.rb:22 # MyError still shows other failure messages

Actual

RSpec throws an internal error when rendering the message and does not render further test failures.

FF

/path/to/rspec-mocks-3.5.0.beta2/lib/rspec/mocks/space.rb:51:in `raise_lifecycle_message': The use of doubles or partial doubles from rspec-mocks outside of the per-test lifecycle is not supported. (RSpec::Mocks::OutsideOfExampleError)
  from /path/to/rspec-mocks-3.5.0.beta2/lib/rspec/mocks/space.rb:11:in `proxy_for'
  from /path/to/rspec-mocks-3.5.0.beta2/lib/rspec/mocks/test_double.rb:111:in `__mock_proxy'
  from /path/to/rspec-mocks-3.5.0.beta2/lib/rspec/mocks/test_double.rb:74:in `method_missing'
  from /path/to/rspec-mocks-3.5.0.beta2/lib/rspec/mocks/proxy.rb:195:in `message_received'
  from /path/to/rspec-mocks-3.5.0.beta2/lib/rspec/mocks/method_double.rb:77:in `proxy_method_invoked'
  from /path/to/rspec-mocks-3.5.0.beta2/lib/rspec/mocks/method_double.rb:64:in `block (2 levels) in define_proxy_method'
  from /path/to/rspec-repro/example_spec.rb:9:in `message'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/formatters/exception_presenter.rb:174:in `exception_lines'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/formatters/exception_presenter.rb:149:in `block in failure_lines'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/formatters/exception_presenter.rb:146:in `tap'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/formatters/exception_presenter.rb:146:in `failure_lines'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/formatters/exception_presenter.rb:32:in `colorized_message_lines'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/formatters/exception_presenter.rb:231:in `formatted_message_and_backtrace'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/formatters/exception_presenter.rb:84:in `fully_formatted_lines'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/formatters/exception_presenter.rb:76:in `fully_formatted'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/notifications.rb:200:in `fully_formatted'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/notifications.rb:114:in `block in fully_formatted_failed_examples'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/notifications.rb:113:in `each'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/notifications.rb:113:in `each_with_index'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/notifications.rb:113:in `fully_formatted_failed_examples'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/formatters/base_text_formatter.rb:33:in `dump_failures'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/reporter.rb:189:in `block in notify'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/reporter.rb:188:in `each'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/reporter.rb:188:in `notify'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/reporter.rb:160:in `block in finish'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/reporter.rb:175:in `close_after'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/reporter.rb:156:in `finish'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/reporter.rb:79:in `report'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/runner.rb:117:in `run_specs'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/runner.rb:93:in `run'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/runner.rb:78:in `run'
  from /path/to/rspec-core-3.5.0.beta2/lib/rspec/core/runner.rb:45:in `invoke'
  from /path/to/rspec-core-3.5.0.beta2/exe/rspec:4:in `<top (required)>'
require 'rspec'
class MyError < StandardError
def initialize(object)
@object = object
end
def message
"I ran into #{@object.body}"
end
end
RSpec.describe MyError do
it "has a message including the object's body" do
object = double(body: "a problem")
# this test case is designed to fail to show the problem
# instead of being notified that a MyError has been raised, we get an exception in RSpec around
# test double lifecycles.
raise MyError.new(object)
end
it "still shows other failure messages" do
# this test failure message will never get rendered because of the above problem
expect(1).to be(2)
end
end
source "https://rubygems.org"
gem "rspec", ">= 3.5.0.beta2"
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.5)
rspec (3.5.0.beta2)
rspec-core (= 3.5.0.beta2)
rspec-expectations (= 3.5.0.beta2)
rspec-mocks (= 3.5.0.beta2)
rspec-core (3.5.0.beta2)
rspec-support (= 3.5.0.beta2)
rspec-expectations (3.5.0.beta2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (= 3.5.0.beta2)
rspec-mocks (3.5.0.beta2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (= 3.5.0.beta2)
rspec-support (3.5.0.beta2)
PLATFORMS
ruby
DEPENDENCIES
rspec (>= 3.5.0.beta2)
BUNDLED WITH
1.10.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment