[rspec-users] ordering dependency in tests?

David Chelimsky dchelimsky at gmail.com
Thu Feb 24 13:48:59 EST 2011


On Feb 24, 2011, at 12:36 PM, Fearless Fool wrote:

> A little additional information: every time I run the tests, it creates 
> a new PremiseGroup model.  Shouldn't the db get rolled back between 
> tests?
> 
> Here is PremiseGroup, listed here in its entirety:
> 
>  class PremiseGroup < ActiveRecord::Base
>    has_many :premise_group_members, :dependent => :destroy
>    has_many :premises, :through => :premise_group_members
>    RESIDENTIAL = self.create(:name => "residential")

^^ this is probably the problem ^^

This line creates a new record in the database every time premise_group.rb is evaluated by the Ruby interpreter. This happens as the app is being bootstrapped and outside any transactions controlled by the Rails testing infrastructure (which RSpec sits on top of).

Options include making that a method instead of a const:

def self.residential
  find_or_create_by_name(:name => "residential")
end

HTH,
David

>  end
> 
> When I put in a debugging statement:
> 
>  before(:each) do
>    $stderr.puts("PremiseGroup count = #{PremiseGroup.count}"
>  end
> 
> ... it reports that there are 151 PremiseGroups, then 152 on the next 
> run, then 153 on the next.  Naturally, calling 'rake db:test:prepare' 
> resets it, but shouldn't the database be rolled back between tests 
> regardless?  I'm not 100% sure this is the cause of my problems, but it 
> smells like it...
> 
> -- 
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list