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?
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users
> 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?
> --
> John Feminella
> Principal Consultant, BitsBuilder
> LI: http://www.linkedin.com/in/fjsquared
> SO: http://stackoverflow.com/users/75170/
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.
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/fjsquared
SO: http://stackoverflow.com/users/75170/
On Thu, Jan 27, 2011 at 17:56, Brian Warner <li...@ruby-forum.com> wrote:
I think that's an interesting idea, David. I whipped up a quick pull
request, which you can see here:
https://github.com/rspec/rspec-core/pull/293
~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/fjsquared
SO: http://stackoverflow.com/users/75170/
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.
But that might just be me.
--
Rick DeNatale
Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
You're absolutely right that it would be confusing for after, and
given that, I think we should probably not add it.
> On Thu, Jan 27, 2011 at 6:16 PM, David Chelimsky <dchel...@gmail.com> wrote:
>> On Jan 27, 2011, at 5:11 PM, John Feminella wrote:
>>
>>> 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).
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.
Cheers,
Wincent
I feel like a third option will cause more confusion, especially if it's just an alias of an existing option!
Pat
1. before(:suite)
2. before(:group)
3. 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
>
> 1. before(:suite)
> 2. before(:group)
> 3. before(:example)
>
> which would reflect the hierarchy of RSpec run (i.e. suite > group > example).
That or :once and :always
SR
> Btw. there is also before(:suite), and working from there wouldn't it make sense to have
>
> 1. before(:suite)
> 2. before(:group)
> 3. 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?