[rspec-users] Difference between :each and :all

260 views
Skip to first unread message

Brian Warner

unread,
Jan 27, 2011, 5:56:28 PM1/27/11
to rspec...@rubyforge.org
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?

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

David Chelimsky

unread,
Jan 27, 2011, 6:16:54 PM1/27/11
to rspec-users
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?

> --
> John Feminella
> Principal Consultant, BitsBuilder
> LI: http://www.linkedin.com/in/fjsquared
> SO: http://stackoverflow.com/users/75170/

John Feminella

unread,
Jan 27, 2011, 6:13:00 PM1/27/11
to rspec...@rubyforge.org
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.


--
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:

Brian Warner

unread,
Jan 27, 2011, 6:28:46 PM1/27/11
to rspec...@rubyforge.org
That does clear it. Thank you =]

John Feminella

unread,
Jan 27, 2011, 9:08:52 PM1/27/11
to rspec-users
> Perhaps :any is a better name? We could add it as an alternative for the same as :all. WDYT?

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/

Rick DeNatale

unread,
Jan 27, 2011, 9:53:30 PM1/27/11
to rspec-users
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). 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

Jon Homan

unread,
Jan 27, 2011, 6:07:23 PM1/27/11
to rspec...@rubyforge.org
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

David Chelimsky

unread,
Jan 27, 2011, 9:58:12 PM1/27/11
to rspec-users
On Thu, Jan 27, 2011 at 8:53 PM, Rick DeNatale <rick.d...@gmail.com> wrote:
> 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).  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.

John Feminella

unread,
Jan 27, 2011, 10:08:25 PM1/27/11
to rspec-users
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.

--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/

John Feminella

unread,
Jan 27, 2011, 6:11:24 PM1/27/11
to rspec...@rubyforge.org
That's not quite right. :each runs before _each_ spec, while :all runs
once, before _any_ spec.
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/fjsquared
SO: http://stackoverflow.com/users/75170/

Wincent Colaiuta

unread,
Jan 27, 2011, 9:57:13 PM1/27/11
to rspec-users
El 28/01/2011, a las 03:53, Rick DeNatale escribió:

> 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

Pat Maddox

unread,
Jan 31, 2011, 3:34:28 AM1/31/11
to rspec-users

I feel like a third option will cause more confusion, especially if it's just an alias of an existing option!

Pat

Evgeniy Dolzhenko

unread,
Jan 31, 2011, 4:38:04 AM1/31/11
to rspec-users
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).

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.

Steven Rogers

unread,
Jan 31, 2011, 9:29:37 AM1/31/11
to rspec-users

On Jan 31, 2011, at 3:38 AM, Evgeniy Dolzhenko wrote:

> 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

David Chelimsky

unread,
Jan 31, 2011, 11:15:58 AM1/31/11
to rspec-users
On Jan 31, 2011, at 3:38 AM, Evgeniy Dolzhenko wrote:

> 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?

Reply all
Reply to author
Forward
0 new messages