Forum: RSpec Understanding before(:all)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
David R. (Guest)
on 2009-01-14 11:48
(Received via mailing list)
I had, for historic reasons, a setup on a group of specs:

  before(:all) do
    @contents = [%w(this that), %w(this other)]
    FasterCSV.stub!(:open).and_return(@contents)
    @filename = 'test_filename'
  end

When I ran the single file (spec spec/transformer/csv_spec.rb), all my
specs passed.  When I ran the whole project's specs together (spec
spec/), these failed.  Changing before(:all) to before(:each) worked,
but for reasons that are not clear to me.  The before(:all) approach
didn't work because the FasterCSV stub wasn't being set.

Can someone explain why there would be different behavior between
running one file and many files and before(:each) vs. before(:all)?
As far as I understand it, I thought before(:all) was supposed to
always run before a group of specs.  Is it possible that a before
(:all) doesn't work right when calling multiple/all specs at the same
time?

Thanks
David C. (Guest)
on 2009-01-14 17:50
(Received via mailing list)
On Sat, Jan 10, 2009 at 12:59 PM, David R.
<removed_email_address@domain.invalid> wrote:
> spec/), these failed.  Changing before(:all) to before(:each) worked,
> but for reasons that are not clear to me.  The before(:all) approach
> didn't work because the FasterCSV stub wasn't being set.
>
> Can someone explain why there would be different behavior between
> running one file and many files and before(:each) vs. before(:all)?
> As far as I understand it, I thought before(:all) was supposed to
> always run before a group of specs.  Is it possible that a before
> (:all) doesn't work right when calling multiple/all specs at the same
> time?

Mocks and stubs get cleared out after each example, so you need to set
them before each example.

before(:all) should run once per group regardless of whether you're
loading one file or many.

HTH,
David
Stephen E. (Guest)
on 2009-01-14 17:51
(Received via mailing list)
On Sat, Jan 10, 2009 at 1:59 PM, David R.
<removed_email_address@domain.invalid> wrote:
>
> Can someone explain why there would be different behavior between
> running one file and many files and before(:each) vs. before(:all)?
> As far as I understand it, I thought before(:all) was supposed to
> always run before a group of specs.

It does, but not necessarily _immediately_ before.  My rough guess
would be that something in one of your other spec files called code
that that required or reloaded FasterCSV, which blew away your stub
when those other spec files were initialized after after this one.

State changes hurt.


--
Have Fun,
   Steve E. (removed_email_address@domain.invalid)
   ESCAPE POD - The Science Fiction Podcast Magazine
   http://www.escapepod.org
This topic is locked and can not be replied to.