[rspec-users] how to pass objects from spec to formatter from before :all block?
dchelimsky at gmail.com
Sat Mar 6 12:28:59 EST 2010
On Sat, Mar 6, 2010 at 11:09 AM, Jarmo Pertman <jarmo.p at gmail.com> wrote:
> I need to pass something from before :all to formatter. I know that i
> could use options hash from spec and then get the value back in
> formatter, but it doesn't work when i'm doing it from before :all.
> So, this work:
> # in spec
> before :each do
> options[:something] = 1
> # in formatter
> def example_failed(example, counter, failure)
> puts example.options[:something] # outputs 1
> But if i try to do the same thing from before :all, then it doesn't
> So i looked into the source of RSpec and in example/example_methods.rb
> in method set_instance_variables_from_hash there is a line which
> ignores some instance variables among with others @_proxy, which has
> this option hash initialized in before :all and that's why it's not
> getting into the formatter - in other words it is just dropped.
> Is this a bug or expected behaviour? If it's expected then what's the
> reason and how could i get the desired results of passing something to
> formatter from before :all?
> Can't we just pass this options hash along?
It was never intended that you would set values on options from inside
an example and access them in a formatter. You've happened on
something that just happens to work because we're using a standard
Ruby data structure.
The reason it won't work for before(:all) is that before(:all) gets
run before any of the examples are run - so it doesn't have access to
the options hash, which is created per example in rspec-1.
What sort of information are you trying to get to your formatter?
Maybe there is a different way to do it.
More information about the rspec-users