Unit testing and stale ActiveRecords


#1

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


#2

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


#3

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


#4

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

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

Jason


#5

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


#6

On 5/1/07, Sean H. 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


#7

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


#8

On 5/2/07, Sean H. 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