I’m having a hard time grasping the difference between :each and :all.
If I have a bunch of stuff inside a “before :each” block. Everytime I
try to run an example that block of code will be run before the example.
Now if I had the same code inside a “before :all” block. Everytime an
example is run, that block will still be run. Yielding the same results.
At least in my mind.
The RSpec book says something like “before :each” defines a state for
each example. “before :all” defines a state for all the examples. But
what’s the difference?
Here’s an illustrative example that should clear things up:
require ‘spec_helper’
describe “behavior of before-each and before-all” do
before(:all) { puts “-- running :all” }
before(:each) { puts “-- running :each” }
describe “addition” do
it “should add two and two” do
(2 + 2).should == 4
end
it "should add three and three" do
(3 + 3).should == 6
end
it "should add four and four" do
(4 + 4).should == 8
end
end
describe “multiplication” do
it “should raise two to two” do
(2 ** 2).should == 4
end
it "should raise three to three" do
(3 ** 3).should == 27
end
it "should raise four to four" do
(4 ** 4).should == 256
end
end
end
And here’s the result:
behavior of before-each and before-all
– running :all
addition
– running :each
should add two and two
– running :each
should add three and three
– running :each
should add four and four
multiplication
– running :each
should raise two to two
– running :each
should raise three to three
– running :each
should raise four to four
Finished in 0.0034 seconds
6 examples, 0 failures
Notice how :each runs before each spec, but :all runs once, before
_any_spec.
That’s not quite right. :each runs before each spec, while :all runs
once, before any spec.
Perhaps :any is a better name? We could add it as an alternative for the same as
:all. WDYT?
Speaking for myself, I never was confused between before(:each) and
before(:all). The first always meant before each OF the examples, and
the latter before all the examples.
As a devil’s advocate, while before(:any) might evoke the current
meaning of before(:all) for some people, after(:any) to me evokes the
curent meaning of after(:each) more than it does after(:all), i.e.
after any OF the examples rather than after all the examples, because
I’d never say after any the examples.
My understanding is that the before :each block runs before every
example.
While before :all blocks run once for the entire example group.
Any side effects in the examples will be persist for objects if you use
a
before :all block. But if you were to use before :each, you guarantee
the
state before every example is run.
Stupid code example.
describe “stuff”
before :all do
puts “done one time”
end
before :each do
puts “done once for every example”
end
describe “one thing stuff does” do
end
describe “second thing stuff does” do
end
end
You’d see something like this in the output:
done one time
done once for every example
done once for every example
Again, this is just my understanding. Could be wrong.
Jon Homan
Not to shoot my own patch in the foot, but my personal opinion is to
have only one way to do it. I think whatever ambiguity there may be in
before(:all) isn’t adequately compensated by the additional confusion
of having before(:any), which sounds like it would do something subtly
different.
El 28/01/2011, a las 03:53, Rick DeNatale escribi:
before(:all).
Same. When you look at them side by side like that, it is pretty clear
what “before each” and “before all” must refer to. Adding a third term
to the mix would actually increase the chance of confusion, IMO.
On Thu, Jan 27, 2011 at 8:53 PM, Rick DeNatale [email protected]
wrote:
before(:all). The first always meant before each OF the examples, and
the latter before all the examples.
As a devil’s advocate, while before(:any) might evoke the current
meaning of before(:all) for some people, after(:any) to me evokes the
curent meaning of after(:each) more than it does after(:all), i.e.
after any OF the examples rather than after all the examples, because
I’d never say after any the examples.
But that might just be me.
You’re absolutely right that it would be confusing for after, and
given that, I think we should probably not add it.
Btw. there is also before(:suite), and working from there wouldn’t it
make sense to have
before(:suite)
before(:group)
before(:example)
which would reflect the hierarchy of RSpec run (i.e. suite > group >
example).
Anyway likely it’s too late to introduce something like this, just my 2
cents, because I’m from
these folks which were always confused about :each/:all and what is the
default, etc. when
I just started speccing.
Btw. there is also before(:suite), and working from there wouldn’t it make sense
to have
before(:suite)
before(:group)
before(:example)
which would reflect the hierarchy of RSpec run (i.e. suite > group > example).
I really, really like this. It maps well to the afters as well, and
therefore does not add to confusion the way :any would.
Anyway likely it’s too late to introduce something like this, just my 2 cents,
because I’m from
these folks which were always confused about :each/:all and what is the default,
etc. when
I just started speccing.
I don’t think it’s too late for this. I’m not convinced to do it yet,
but I think this is a solid, clear direction.
More opinions on this?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.