# Rails /w Ruby 1.9.1p243 - Unit Test assert\_equal question

**URL:** <https://www.ruby-forum.com/t/rails-w-ruby-1-9-1p243-unit-test-assert-equal-question/177385>\
**Category:** Rails\
**Created:** [October 16, 2009, 3:02am UTC](https://www.ruby-forum.com/t/rails-w-ruby-1-9-1p243-unit-test-assert-equal-question/177385 "2009-10-16T03:02:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mojo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mojo](https://www.ruby-forum.com/u/mojo)\
**Post date:** [October 16, 2009, 3:02am UTC](https://www.ruby-forum.com/t/rails-w-ruby-1-9-1p243-unit-test-assert-equal-question/177385/1 "2009-10-16T03:02:50Z")

</div>

Hi everyone

I am new to Rails. My platform is Ubuntu 9.04 with Ruby 1.9.1p243 and  
Rails 2.3.4. I am reading SitePoint’s Simply Rails 2 book. On page  
240, the author shows how to test the associations between Story and  
Vote model.

The code is below

## app/models/story.rb:

class Story \< ActiveRecord::Base  
validates\_presence\_of :name, :link  
has\_many :votes  
end

## app/models/vote.rb:

class Vote \< ActiveRecord::Base  
belongs\_to :story  
end

## test/fixtures/stories.yml:

one:  
name: My Shiny Weblog  
link: [http://www.google.com/](http://www.google.com/)

two:  
name: Another stupid site  
link: [http://www.apple.com/](http://www.apple.com/)

## test/fixtures/votes.yml:

one:  
story: one

two:  
story: one

## test/unit/story\_test.rb:

def test\_should\_have\_a\_votes\_association  
assert\_equal [votes(:one), votes(:two)], stories(:one).votes  
end

$ rake test:units

The output of assert return failure. So I do trial-and-error fix and  
manage to fix it by swap the position of votes(:one) and votes(:two)  
order so the test method become:  
assert\_equal [votes(:two), votes(:one)], stories(:one).votes  
Can some Rails experts explain to me why the order of vote inside the  
array could affect the assert\_equal measure?  
Thank you so much

---

<div class="post-metadata">

**Author:** ![mojo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mojo](https://www.ruby-forum.com/u/mojo)\
**Post date:** [October 16, 2009, 3:52am UTC](https://www.ruby-forum.com/t/rails-w-ruby-1-9-1p243-unit-test-assert-equal-question/177385/2 "2009-10-16T03:52:03Z")

</div>

On Oct 16, 1:31 am, mojo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can some Rails experts explain to me why the order of vote inside the  
> array could affect the assert\_equal measure?  
> Thank you so much

because arrays are fundamentally ordered collections so for an array  
== means same elements and in the same order. Whenever you get  
something out of the database and there is no order clause (eg if you  
do Vote.all, or the association you have here) the database is free to  
return stuff in any order it wants to. Quite often it will be mostly  
the same order each time but you shouldn’t rely on that (more often  
than not it will something to do with the order things are stored in  
on disk.

Fred
