SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simply Rails 2: Problem Testing a Vote's Relationship

    I've been at this for several hours now, and I read some related threads in this forum about this issue....but that's still not resolving my problem.

    I am on page 242 of Chapter 7 and running into this problem:

    Code:
    shovell freddyrodriguez$ rake test:units
    (in /Users/freddyrodriguez/RailsApps/shovell)
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -Ilib:test "/Library/Ruby/Gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/unit/helpers/stories_helper_test.rb" "test/unit/helpers/votes_helper_test.rb" "test/unit/story_test.rb" "test/unit/vote_test.rb" 
    Loaded suite /Library/Ruby/Gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
    Started
    ..F..F.
    Finished in 0.101577 seconds.
    
      1) Failure:
    test_should_have_a_votes_association(StoryTest) [/test/unit/story_test.rb:23]:
    <[#<Vote id: 2053932785, story_id: 2053932785, created_at: "2009-09-04 05:15:06", updated_at: "2009-09-04 05:15:06">,
     #<Vote id: 298486374, story_id: 2053932785, created_at: "2009-09-04 05:15:06", updated_at: "2009-09-04 05:15:06">]> expected but was
    <[#<Vote id: 298486374, story_id: 2053932785, created_at: "2009-09-04 05:15:06", updated_at: "2009-09-04 05:15:06">, #<Vote id: 2053932785, story_id: 2053932785, created_at: "2009-09-04 05:15:06", updated_at: "2009-09-04 05:15:06">]>.
    
      2) Failure:
    test_should_return_highest_vote_id_first(StoryTest) [/test/unit/story_test.rb:27]:
    <#<Vote id: 298486374, story_id: 2053932785, created_at: "2009-09-04 05:15:06", updated_at: "2009-09-04 05:15:06">> expected but was
    <#<Vote id: 2053932785, story_id: 2053932785, created_at: "2009-09-04 05:15:06", updated_at: "2009-09-04 05:15:06">>.
    
    7 tests, 7 assertions, 2 failures, 0 errors
    rake aborted!
    Command failed with status (1): [/System/Library/Frameworks/Ruby.framework/...]
    
    (See full trace by running task with --trace)
    I tried looking through all of the files that I created and I couldn't find where I went wrong.

    I then copied and pasted all of the files from the code archive and I'm still getting the same problem.

    Does anyone know how to troubleshoot this issue?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,237
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    Hi freddy33r, welcome to the forums,

    It looks like the 2 are in the wrong order.

    If your story_test.rb looks like:
    Code Ruby:
      def test_should_have_a_votes_association
        assert_equal [ votes(:one), votes(:two) ], stories(:one).votes
      end
      def test_should_return_highest_vote_id_first
        assert_equal votes(:two), stories(:one).votes.latest.first
      end

    And your stories.yml looks like:
    Code:
    one:
      name: My shiny weblog 
      link: http://poocs.net/ 
    
    two:
      name: SitePoint Forums 
      link: http://www.sitepoint.com/forums/
    Does your votes.yml look like:
    Code:
    one:
      story: one
    
    two:
      story: one
    ??

  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep...that's how my files look. I copied them from the code archive.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,237
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    It's a puzzle to me. I notice the vote id values don't have the same number of characters

    2053932785
    298486374

    So if there was a "0" at the end of the shorter one, it would be right. But AFAIK these numbers are generated by Rails, so I wonder what's up.

  5. #5
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah...I noticed that too. I really don't know how to fix that.

    But...since it's a test and this is only a test application, I'm just going to chug along in the book. I hope this doesn't cause the whole application to not function.

  6. #6
    SitePoint Zealot phillipus_rex's Avatar
    Join Date
    Jan 2007
    Location
    Silicon Holler
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Freddy,

    What version of rails are you running?

    I'm not sure what the exact version is for the sitepoint book, but I've found that I get random errors if I'm using a book and not using the exact version of ruby, rails, gems, etc...
    "Be kind, for everyone you meet is fighting a
    hard battle." -Plato

  7. #7
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Freddy,

    I have the exact same problem as you, same numbers, in fact. I'm running rails 2.3.3.

    What's disconcerting is that when reading through the tests, I would test the same way if I were writing the code myself..... and not following the book. I'm going to proceed with the book too, but I'd love to figure out the problem so that I don't run into it again when writing my own applications. This seems like a very common test you would want to run.... I can see many many applications where you would want an ordered list and the test to check that ordered list.

    I tried to run the Shovell application that comes in the code archive to see if it had the same problem, even installed rails 2.0.2, but I'm running into problems running it... I'll keep at it.

    Nick

    edit: as a side note, I'm using Aptana for the development on this application, I don't think that has an affect, but I'm curious if Freddy is doing the same thing?

  8. #8
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm getting the same problem - just posted the issue in another thread - apologies for that.

    I'm going to plough on with the book too - would love to know what's going on though

  9. #9
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I managed to solve my problem, but don't have enough posts to be able to post the solution!

  10. #10
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only way I could get it to work was to specify id's of both the stories and the votes:

    stories.yml
    Code:
    one:
      id: 1    
      name: My shiny weblog
      link: http://poocs.net/
      user: patrick
      
    two:
      id: 2
      name: SitePoint Forums
      link: http://www.sitepoint.com/forums/
      user: patrick
    votes.yml
    Code:
    one:
      id: 1
      story_id: 1  
      user: patrick
      
    two:
      id: 2    
      story_id: 1
      user: john
    A ruby developer friend of mine came to this conclusion after reading this which explains that fixtures use hash id's so can't be depended upon for use in ordering.

    Hope this is of use to someone, or if anyone has better solutions please feel free to contribute

  11. #11
    Team ********* Louis Simoneau's Avatar
    Join Date
    Sep 2009
    Location
    Melbourne, Australia
    Posts
    228
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi guys,

    I've just had a good long look at the code in chapter 7, prompted by f00tey's posts.

    The problem is that we're trying to determine the "latest" story based on its id, but Rails 2 fixtures don't require explicitly setting ids, so they're generated automatically by the app. This is unreliable: it doesn't guarantee that the record that's later in the fixtures file will have a higher numerical id.

    f00tey's solution of reverting to Rails 1.x style fixtures with specified ids works, but I think an even better solution would be to refactor the latest method to use created_at rather than id: that way we're sure that we're actually looking at the more recent stories:

    Code Ruby:
    def latest
      find :all, :order => 'created_at DESC', :limit => 3
    end

    Then we just need to add dynamic created_at values to our fixtures:

    Code:
    one:
      name: My shiny weblog
      link: http://poocs.net/
      created_at: <%= 3.days.ago %>
    
    two:
      name: SitePoint Forums
      link: http://www.sitepoint.com/forums/
      created_at: <%= 1.day.ago %>
    This way we're sure that our app is doing what we want to do (selecting the last stories added), and that we're testing that behavior accurately. I want to write up a version of this fix and put it on the book's errata page, but I thought I'd check what you guys thought about it first.

  12. #12
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,237
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    I agree that using "highest_vote_id" for "latest_story" is a bit imprecise.
    I didn't realize you could add dynamic code to the fixture files, and I don't know how much work would be involved in correcting all references to "highest_vote_id". But even if only a mention in the errata, it definately sounds like a worthwhile improvement for the next book version.

  13. #13
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Louis: I agree that it would make more sense to go off of the created_at field rather than the ID.

    Though, just a quick question. At this point in the book, aren't we talking about latest votes, and not latest stories? In your example, you've added the dynamic ruby code to the fixtures, but to the stories fixture and not the votes.

    Shouldn't it the modifications to the code be made to the votes fixtures, and test be re-written accordingly at this point in the book?

    PS. F00tey solution worked for me.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •