# Unit testing and stale ActiveRecords

**URL:** <https://www.ruby-forum.com/t/unit-testing-and-stale-activerecords/103051>\
**Category:** Rails\
**Created:** [May 1, 2007, 7:55am UTC](https://www.ruby-forum.com/t/unit-testing-and-stale-activerecords/103051 "2007-05-01T07:55:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [May 1, 2007, 7:55am UTC](https://www.ruby-forum.com/t/unit-testing-and-stale-activerecords/103051/1 "2007-05-01T07:55:14Z")

</div>

I’m having real trouble unit testing my Rails ActiveRecord models. It  
seems like more than half the time the data is stale. All I have to do  
is reference a variable and it suddenly the test will start passing.  
I’m not using the yml fixtures much because much of the behavior in my  
models deals with the relationships they have with other model objects  
and the yml fixtures don’t seem to easily support this.

- has\_many array fields are sometimes populated
- has\_one fields are sometimes populated
- referencing an active record field sometimes causes it to be populated  
later
- calling “save” then “reload” doesn’t help
- find(:all, :conditions =\> []) doesn’t seem to work much in tests

I think it has something to do with constructing model objects  
dynamically from functions I call within my tests.

Am I approaching this wrong? I’m fairly new to Ruby and Rails but very  
experienced with unit testing and TDD. This is not giving me much  
confidence. It’s hard to justify writing unit tests when they don’t  
seem behave at all like the production version.

What gives?

–  
Sean

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [May 1, 2007, 8:45am UTC](https://www.ruby-forum.com/t/unit-testing-and-stale-activerecords/103051/2 "2007-05-01T08:45:34Z")

</div>

Well Sean…

First of all, let me tell you that you are a very, but very bad computer  
programming guy. You are stupid, ignorant and you never disserved to be  
born within 500 miles of a computer.

The key to your problem is soooooooooo simply. You have to use the  
sqrtzm fixtures instead of the yml fixtures. I think it is ubvious. You  
are wasting people’s time here. Please behave.

Sincerely,

Joshua

Sean H. wrote:

> I’m having real trouble unit testing my Rails ActiveRecord models. It  
> seems like more than half the time the data is stale. All I have to do  
> is reference a variable and it suddenly the test will start passing.  
> I’m not using the yml fixtures much because much of the behavior in my  
> models deals with the relationships they have with other model objects  
> and the yml fixtures don’t seem to easily support this.
> 
> - has\_many array fields are sometimes populated
> - has\_one fields are sometimes populated
> - referencing an active record field sometimes causes it to be populated  
> later
> - calling “save” then “reload” doesn’t help
> - find(:all, :conditions =\> []) doesn’t seem to work much in tests
> 
> I think it has something to do with constructing model objects  
> dynamically from functions I call within my tests.
> 
> Am I approaching this wrong? I’m fairly new to Ruby and Rails but very  
> experienced with unit testing and TDD. This is not giving me much  
> confidence. It’s hard to justify writing unit tests when they don’t  
> seem behave at all like the production version.
> 
> What gives?
> 
> –  
> Sean

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [May 1, 2007, 9:28am UTC](https://www.ruby-forum.com/t/unit-testing-and-stale-activerecords/103051/3 "2007-05-01T09:28:09Z")

</div>

To be more specific…

model.things.find(:all)[0]  
and  
mode.things[0]

do not return the same instance (the #\<Thing: 0x???\> values are  
different)

Most of the fields have the same values, including the ‘id’ field, but  
others such as belongs\_to relationships are nil. Obviously if I change  
one instances data it doesn’t change the other.

I thought Rails was supposed to ensure the coherency of objects of the  
same id.

–  
Sean

Sean H. wrote:

> I’m having real trouble unit testing my Rails ActiveRecord models. It  
> seems like more than half the time the data is stale. All I have to do  
> is reference a variable and it suddenly the test will start passing.  
> I’m not using the yml fixtures much because much of the behavior in my  
> models deals with the relationships they have with other model objects  
> and the yml fixtures don’t seem to easily support this.
> 
> - has\_many array fields are sometimes populated
> - has\_one fields are sometimes populated
> - referencing an active record field sometimes causes it to be populated  
> later
> - calling “save” then “reload” doesn’t help
> - find(:all, :conditions =\> []) doesn’t seem to work much in tests
> 
> I think it has something to do with constructing model objects  
> dynamically from functions I call within my tests.
> 
> Am I approaching this wrong? I’m fairly new to Ruby and Rails but very  
> experienced with unit testing and TDD. This is not giving me much  
> confidence. It’s hard to justify writing unit tests when they don’t  
> seem behave at all like the production version.
> 
> What gives?
> 
> –  
> Sean

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [May 1, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/unit-testing-and-stale-activerecords/103051/4 "2007-05-01T14:56:00Z")

</div>

Not sure what Joshua’s problem is, drunk maybe?

Anyway, why aren’t you using Fixtures? This is exactly what they’re for.

Jason

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [May 1, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/unit-testing-and-stale-activerecords/103051/5 "2007-05-01T18:41:38Z")

</div>

It’s just way rails is I’m afraid -  
a = Model.find 1  
b = Model.find 2

creates 2 separate and unrelated instances of Model - there is no magic  
that propagates changes from a to b

I’m not sure why you can’t use fixtures though.

Fred

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [May 2, 2007, 6:52am UTC](https://www.ruby-forum.com/t/unit-testing-and-stale-activerecords/103051/6 "2007-05-02T06:52:32Z")

</div>

On 5/1/07, Sean H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> club.memberships[0] are not populated; let alone user.clubs[0] or  
> club.users[0] (the through relationships) No matter how many ways I  
> save and load the active records nothing seems to work as expected.

Could you paste an example of a problematic unit test and  
corresponding production code?

Pat

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [May 2, 2007, 6:28am UTC](https://www.ruby-forum.com/t/unit-testing-and-stale-activerecords/103051/7 "2007-05-02T06:28:29Z")

</div>

Thanks for the reply Frederick,

How am I supposed to use fixtures to test the creation of dynamic  
objects? I can’t figure out a reliable method of save/load that works.  
For example, lets say I have a user model. This user has a method for  
joining a club which is represented by a memberships ‘through’ table. I  
want to test that calling user.join\_club© creates a membership record  
and binds it to the correct user and club. However, when I construct  
that new object and save it to the database both user.memberships[0] and  
club.memberships[0] are not populated; let alone user.clubs[0] or  
club.users[0] (the through relationships) No matter how many ways I  
save and load the active records nothing seems to work as expected.

The other problem I have with yml fixtures is that it’s hard to build  
relationships with them since they only represent one table. I guess I  
could manually connect them with the ‘id’ field across multiple yml  
files, but that’s pretty clunky compared to creating only what you need  
in the specific test. It also means _lots_ of objects in my fixtures  
which clutters things up and makes it unclear what exactly is required  
to demonstrate the functionality being tested.

I’ve been searching for information about best-practices in unit testing  
rails and this information just doesn’t seem to exist. Most of the  
examples are pretty brain-dead and don’t deal with relationships and  
object interactions which is where most of the bugs and interesting  
design problems are.

–  
Sean

Frederick C. wrote:

> It’s just way rails is I’m afraid -  
> a = Model.find 1  
> b = Model.find 2
> 
> creates 2 separate and unrelated instances of Model - there is no magic  
> that propagates changes from a to b
> 
> I’m not sure why you can’t use fixtures though.
> 
> Fred

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [May 2, 2007, 2:54pm UTC](https://www.ruby-forum.com/t/unit-testing-and-stale-activerecords/103051/8 "2007-05-02T14:54:29Z")

</div>

On 5/2/07, Sean H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> that new object and save it to the database both user.memberships[0] and  
> club.memberships[0] are not populated; let alone user.clubs[0] or  
> club.users[0] (the through relationships) No matter how many ways I  
> save and load the active records nothing seems to work as expected.

Would like to see your code. This sounds like you don’t have  
relationships  
set up correctly.

The other problem I have with yml fixtures is that it’s hard to build

> relationships with them since they only represent one table. I guess I  
> could manually connect them with the ‘id’ field across multiple yml  
> files, but that’s pretty clunky compared to creating only what you need  
> in the specific test. It also means _lots_ of objects in my fixtures  
> which clutters things up and makes it unclear what exactly is required  
> to demonstrate the functionality being tested.

It sounds like you don’t like thinking about the database. Unless you  
have  
an absolute ton of relationships (questionable in itself, of course),  
it’s  
not hard to put together data exactly as you want for testing. You _do_  
need  
to understand what goes on in the database itself and what ActiveRecord  
expects to properly use Rails with AR.

You are of course able to write ActiveRecord code to build objects (such  
as  
in setup, or in a separate file required in tests), but that requires  
that  
your relationships are set up correctly. If you’re using ActiveRecord,  
you  
_need_ to manually set up data in fixtures to test your relationships.

Is there something you’re confused about with fixtures or ActiveRecord  
testing?

I’ve been searching for information about best-practices in unit testing

> rails and this information just doesn’t seem to exist. Most of the  
> examples are pretty brain-dead and don’t deal with relationships and  
> object interactions which is where most of the bugs and interesting  
> design problems are.
> 
> –  
> Sean

class MyModelTest \< Test::Unit::TestCase  
fixtures :my\_models, :pieces

def test\_has\_many\_pieces  
model = MyModel.new  
assert model.respond\_to?(:pieces)  
…  
assert\_equal 3, model.pieces.size  
…  
end  
end

Please define “brain-dead”. It unit-testing supposed to be witty and  
exciting and something hard to figure out? Or do you need a tutorial on  
unit-testing itself?

Jason
