# Ordering dependency in tests?

**URL:** <https://www.ruby-forum.com/t/ordering-dependency-in-tests/203669>\
**Category:** RSpec\
**Created:** [February 24, 2011, 6:55pm UTC](https://www.ruby-forum.com/t/ordering-dependency-in-tests/203669 "2011-02-24T18:55:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 24, 2011, 6:55pm UTC](https://www.ruby-forum.com/t/ordering-dependency-in-tests/203669/1 "2011-02-24T18:55:18Z")

</div>

I’m baffled. If I do:

$ bundle exec ruby -S rspec --tty A\_spec.rb  
$ bundle exec ruby -S rspec --tty B\_spec.rb

I get no errors. But then if I do:

$ bundle exec ruby -S rspec --tty A\_spec.rb B\_spec.rb

I get an error on B\_spec. And if I reverse the order:

$ bundle exec ruby -S rspec --tty B\_spec.rb A\_spec.rb

I get an entirely different error on A\_spec. I swear that I’m not doing  
any before(:all) anywhere, but clearly there’s some state that is  
persisting between the two spec files. I’ve tried inserting  
$stderr.puts() messages to gain some insight as to what’s happening, but  
they seem to be suppressed (is that expected?).

So: any ideas of gotchas to look out for?

In the meantime, I’m going to start commenting out blocks of tests in  
A\_spec and see when B\_spec stops failing.

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [February 24, 2011, 7:18pm UTC](https://www.ruby-forum.com/t/ordering-dependency-in-tests/203669/2 "2011-02-24T19:18:58Z")

</div>

On Feb 24, 2011, at 11:55 AM, Fearless F. wrote:

> $ bundle exec ruby -S rspec --tty B\_spec.rb A\_spec.rb
> 
> I get an entirely different error on A\_spec.

What are the failures you’re seeing?

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [February 24, 2011, 7:36pm UTC](https://www.ruby-forum.com/t/ordering-dependency-in-tests/203669/3 "2011-02-24T19:36:48Z")

</div>

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”)  
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…

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [February 24, 2011, 7:49pm UTC](https://www.ruby-forum.com/t/ordering-dependency-in-tests/203669/4 "2011-02-24T19:49:49Z")

</div>

On Feb 24, 2011, at 12:36 PM, Fearless F. 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

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [February 24, 2011, 7:44pm UTC](https://www.ruby-forum.com/t/ordering-dependency-in-tests/203669/5 "2011-02-24T19:44:44Z")

</div>

David C. wrote in post #983675:

> On Feb 24, 2011, at 11:55 AM, Fearless F. wrote:  
> What are the failures you’re seeing?

When running A before B, B’s assertion that:  
@common\_options[:premise\_group].premises.size.should == 3  
fails because premise\_group.premises.size == 5 (even though only 3  
premises have been created).

When running B before A, A’s assertion that:  
e1.ranking(premises[0]).should be\_within(0.0001).of(0.0)  
fails because a call to sort() fails because there is a nil in some list  
that ought to be a Float. (I haven’t dug deeper into that one.)

But to reiterate, either tests completes without error when run alone.  
Still digging…

- ff

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [February 24, 2011, 7:55pm UTC](https://www.ruby-forum.com/t/ordering-dependency-in-tests/203669/6 "2011-02-24T19:55:29Z")

</div>

cattr\_accessor, class vars / class instance vars, constants,  
globals…all things that maintain state across test runs, and so could  
lead to errors like this. I’d start looking there.

Pat

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [February 24, 2011, 7:58pm UTC](https://www.ruby-forum.com/t/ordering-dependency-in-tests/203669/7 "2011-02-24T19:58:03Z")

</div>

David C. wrote in post #983690:

> > RESIDENTIAL = self.create(:name =\> “residential”)  
> > ^^ this is probably the problem ^^  
> > …

Ah! got it. FWIW, I wrote a query to the Rails group several months  
ago wondering if this construct was legit and got feedback that it was  
legit. Your explanation makes good sense. I’ll change that and see  
what happens.

If you don’t hear back from me, assume it’s working! 😉 And thanks in  
advance.

- ff

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [February 24, 2011, 7:53pm UTC](https://www.ruby-forum.com/t/ordering-dependency-in-tests/203669/8 "2011-02-24T19:53:09Z")

</div>

yet more info…

The two errors appear to be closely related. When A and B are run as  
individual  
tests,  
PremiseGroup#premises  
is returning expected values. When run together, premise\_group.premises  
returns “phantom” premises (which show up as nil values). For test B,  
the phantoms causes the count to be wrong. For test A, the nil values  
cause nils to show up in a list that is passed to sort().

I’m still baffled, but I must be closer to the answer.

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [February 24, 2011, 8:20pm UTC](https://www.ruby-forum.com/t/ordering-dependency-in-tests/203669/9 "2011-02-24T20:20:00Z")

</div>

On Feb 24, 2011, at 12:58 PM, Fearless F. wrote:

> David C. wrote in post #983690:
> 
> > > RESIDENTIAL = self.create(:name =\> “residential”)  
> > > ^^ this is probably the problem ^^  
> > > …
> 
> Ah! got it. FWIW, I wrote a query to the Rails group several months  
> ago wondering if this construct was legit and got feedback that it was  
> legit.

I think it’s OK as a const with find\_or\_create, but with find, it is  
clearly problematic.

Cheers,  
David

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [February 24, 2011, 8:38pm UTC](https://www.ruby-forum.com/t/ordering-dependency-in-tests/203669/10 "2011-02-24T20:38:07Z")

</div>

SOLVED – it was an AR caching problem. I had Factory code that was  
essentially doing:

def make\_me\_a\_premise(opts = {})  
opts = MODEL\_PREMISE\_DEFAULTS.merge(opts)  
premise = Factory(:premise)  
Factory(:premise\_group\_member,  
:premise =\> premise,  
:premise\_group =\> opts[:premise\_group])  
…  
premise  
end

… which was properly creating a premise \<=\> premise\_group association,  
but not informing the in-memory premise\_group about the new association.  
adding:

```
opts[:premise_group].reload

```

fixed everything. @David and @Pat, thanks for helping point the way.

- ff

[PS: Is there a more Rails-y way do make the association that won’t run  
into this problem? And what about the case where the association  
carries a value with it – you can’t just do a push.]

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [February 24, 2011, 8:24pm UTC](https://www.ruby-forum.com/t/ordering-dependency-in-tests/203669/11 "2011-02-24T20:24:19Z")

</div>

@Pat: thanks for the suggestions.

It’s perhaps not only what David suggested – I seem to be suffering  
from some cached AR value. I changed my constant declaration to:

RESIDENTIAL = find\_or\_create\_by\_name(“residential”)

This keeps the number of instances down to a 1. That’s good. But if I  
put this in the setup of my test:

puts(@premise\_group.premises.size)  
@premise\_group.reload  
puts(@premise\_group.premises.size

I get

8  
0

Since my setup code is pushing elements onto @premise\_group.premises, I  
can see that this would cause trouble when @premise\_group is written out  
to the DB. My head hurts just thinking about where I need to sprinkle  
reload() statements in my code – maybe the coffee will kick in and  
clarity will result.
