[rspec-devel] [ rspec-Bugs-11231 ] fixtures don't load with before(:all)

noreply at rubyforge.org noreply at rubyforge.org
Wed May 30 14:19:43 EDT 2007


Bugs item #11231, was opened at 2007-05-30 09:52
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=11231&group_id=797

Category: None
Group: None
Status: Open
Resolution: None
Priority: 3
Submitted By: Jeremy Stephens (viking415)
Assigned to: Nobody (None)
Summary: fixtures don't load with before(:all)

Initial Comment:
Fixtures only load when using before(:each).  If I use before(:all), they don't get loaded at all.

To reproduce this, run these commands in a shell:

rails -d sqlite3 jungle
cd jungle
ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec
ruby script/plugin install svn://rubyforge.org/var/svn/rspec/tags/CURRENT/rspec_on_rails
script/generate rspec
script/generate rspec_model monkey name:string
rake db:migrate


Then edit spec/models/monkey_spec.rb to include the following:
#############
require File.dirname(__FILE__) + '/../spec_helper'

describe "monkey with a before(:each)" do
  fixtures :monkeys

  before(:each) do
    @monkey = Monkey.find_by_id(1)
  end

  it "should not be nil" do
    @monkey.should_not be_nil
  end
end

describe "monkey with a before(:all)" do
  fixtures :monkeys

  before(:all) do
    @monkey = Monkey.find_by_id(1)
  end

  it "should not be nil" do
    @monkey.should_not be_nil
  end
end
#############

Run rake spec:models, and watch the first pass and the second fail.  I'm running Kubuntu (Feisty) with Rails 1.2.3 and RSpec SVN r2060 (1.0.4).

----------------------------------------------------------------------

>Comment By: Jeremy Stephens (viking415)
Date: 2007-05-30 13:19

Message:
My original reason for wanting to use fixtures in
before(:all) was to test a userstamp plugin for
Rails/ActiveRecord which automatically sets a 'created_by'
or 'updated_by' field when a record is saved.  In my tests,
I wanted to login an existing user before each example, and
it seemed wasteful to me at the time for that login to
happen every time.  It really isn't a big deal to use
before(:each) instead; I guess it just struck me as odd that
fixtures weren't available for before(:all).

After reading your argument, though, I see your point.  I
think if you choose to outlaw using fixtures during a
before(:all), an error message would definitely be helpful.
Thanks!

----------------------------------------------------------------------

Comment By: David Chelimsky (dchelimsky)
Date: 2007-05-30 11:51

Message:
This was brought up a while back (using the old syntax): http://rubyforge.org/tracker/index.php?func=detail&aid=8145&group_id=797&atid=3149

The basic problem is that fixtures get rolled back between each example if you're using transactional fixtures (which seems to be the way to go). So if we support fixtures in before(:all) and you create @monkey, when the fixtures roll back the @monkey will remain the same - keeping its state through each example. This goes against conventional wisdom, which suggests that state should not be shared between examples.

So my instinct is to implement fixtures in before(:all) such that it raises an error saying you can't do it and should use before(:each). But I'm open to other thoughts on the matter, so please feel free to make a case.

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=11231&group_id=797


More information about the rspec-devel mailing list