Topic: [SOLVED] Newbie having trouble unit testing the has_many association

Hi all,

I am learning RoR using the "Build Your Own Ruby on Rails Web Applications" ebook by Patrick Lenz. I figured that the testing methodology for RoR has changed since then because we do not use the same syntax in the ebook as what I read in the rails documentation.

Anyway, I read carefully the Rails doc on the website, and managed to do my first basic tests without problem.
Since, I added some a has_many association in my database, and I have some trouble with unit testing!

Here is what I have:

My Story model:

class Story < ActiveRecord::Base
  validates_presence_of :name, :link
  has_many :votes
  def latest_votes
    votes.find(:all, :order => 'id DESC', :limit => 3) 
  end
end

-In my story_test.rb:

test "votes association" do
    assert_equal [votes(:first), votes(:second) ],
                stories(:first).votes
end

and my fixtures in test/fixtures:

-stories.yml:

# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

first: 
    name: My shiny weblog 
    link: http://poocs.net/

second: 
    name: SitePoint Forums 
    link: http://www.sitepoint.com/forums/

-votes.yml:

first: 
    id: 1
    story_id: 1
    created_at: <%= Time.now.to_s(:db) %> 
    
second:
    id: 2 
    story_id: 1 
    created_at: <%= Time.now.to_s(:db) %>


Then I want to do my unit tests, so I type in my terminal rake test:units or, from the test folder, ruby unit/story_test.rb, and I get this as a result:

Failure:
test_votes_association(StoryTest) [unit/story_test.rb:24]:
<[#<Vote id: 1, story_id: 1, created_at: "2010-06-07 12:10:30", updated_at: "2010-06-07 16:10:30">,
 #<Vote id: 2, story_id: 1, created_at: "2010-06-07 12:10:30", updated_at: "2010-06-07 16:10:30">]> expected but was
<[]>.

In other words, it seems that stories(:first).votes is NIL in the TEST environment..
While my application seems to work fine in the DEVELOPMENT environment.

Isn't my votes fixtures supposed to generate the corresponding entries into the test database?
At first I thought I needed to do something like migrate my database schema with the new association to the test environment (rake db:migrate RAILS_ENV="test"), but it does not work either.

Am I mixing everything up? Did I do something wrong??

Thanks a lot!

I would not like to go further in the development of this application without knowing why my test is failing..


David

Last edited by freekers (2010-06-09 10:32:26)

Re: [SOLVED] Newbie having trouble unit testing the has_many association

Please .. anyone??

Re: [SOLVED] Newbie having trouble unit testing the has_many association

Explicitly set the id of the story fixtures, You'll probably find that the story you are expecting to associate the votes with has an id value other than 1

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: [SOLVED] Newbie having trouble unit testing the has_many association

Thanks very much Jamesw!

That was right! Now that I have working tests I feel much more comfortable to add functionality and continue learning RoR!