SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Location
    Reigate and Bath, UK.
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Simply Rails 2, pg 242 testing problem

    I don't know if anyone else going through the book has had this problem, hopefully someone can help me out!

    I've just run
    Code:
    rake test:units
    (as on pg 242) after adding the tests to test a vote's relationship to a story and the voting history order, and rather than all the tests passing, like in the screenshot, I've got 3 failures!

    I don't think I'm good enough to understand the errors I got given. Can anybody please help me?!

    Code:
    Macintosh-9:shovell Laura$ rake test:units
    (in /Users/Laura/shovell)
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -Ilib:test "/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb" "test/unit/story_test.rb" "test/unit/vote_test.rb" 
    Loaded suite /Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader
    Started
    ..F..FF
    Finished in 0.127123 seconds.
    
      1) Failure:
    test_should_have_a_votes_association(StoryTest)
        [./test/unit/story_test.rb:23:in `test_should_have_a_votes_association'
         /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
         /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in `run']:
    <[#<Vote id: 953125641, story_id: 1, created_at: "2008-07-11 13:25:45", updated_at: "2008-07-11 13:25:45">,
     #<Vote id: 996332877, story_id: 1, created_at: "2008-07-11 13:25:45", updated_at: "2008-07-11 13:25:45">]> expected but was
    <[]>.
    
      2) Failure:
    test_should_return_highest_vote_id_first(StoryTest)
        [./test/unit/story_test.rb:27:in `test_should_return_highest_vote_id_first'
         /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
         /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in `run']:
    <#<Vote id: 996332877, story_id: 1, created_at: "2008-07-11 13:25:45", updated_at: "2008-07-11 13:25:45">> expected but was
    <nil>.
    
      3) Failure:
    test_story_association(VoteTest)
        [./test/unit/vote_test.rb:5:in `test_story_association'
         /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
         /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in `run']:
    <#<Story id: 953125641, name: "My shiny weblog", link: "http://poocs.net/", created_at: "2008-07-11 13:25:45", updated_at: "2008-07-11 13:25:45">> expected but was
    <nil>.
    
    7 tests, 7 assertions, 3 failures, 0 errors
    rake aborted!
    Command failed with status (1): [/System/Library/Frameworks/Ruby.framework/...]

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please post the code for the votes model, the stories model and the unit tests that fail (test_should_have_a_votes_association and test_should_return_highest_vote_id_first in StoryTest, test_story_association in VoteTest).

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Location
    Reigate and Bath, UK.
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    story.rb

    Code:
    class Story < ActiveRecord::Base
      validates_presence_of :name, :link
      has_many :votes do
        def latest
          find :all, :order => 'id DESC', :limit => 3
        end
      end
      def to_param
        "#{id}-#{name.gsub(/\W/, '-').downcase}"
      end
    end
    vote.rb

    Code:
    class Vote < ActiveRecord::Base
      belongs_to :story
    end
    story_test.rb

    Code:
    require 'test_helper'
    
    class StoryTest < ActiveSupport::TestCase
      
      def test_should_not_be_valid_without_name
        s = Story.create(:name => nil, :link => 'http://www.testsubmission.com/')
        assert s.errors.on(:name)
      end
      
      def test_should_be_valid_without_link
        s = Story.create(:name => 'My test submission', :link => nil)
        assert s.errors.on(:link)
      end
      
      def test_should_create_story
        s = Story.create(
          :name => 'My test submission',
          :link => 'http://www.testsubmission.com/')
        assert s.valid?
      end
    
      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
    
      def test_should_return_3_latest_votes
        10.times { stories(:one).votes.create }
        assert_equal 3, stories(:one).votes.latest.size
      end
      
    end
    vote_test.rb

    Code:
    require 'test_helper'
    
    class VoteTest < ActiveSupport::TestCase
      
      def test_story_association
        assert_equal stories(:one), votes(:one).story
      end
      
    end
    thanks!

  4. #4
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    There might be something wrong in your 'votes.yml' in test/fixtures...

    Check the reference to the Story

    If it says "story_id: one", then change it to 'story: one'

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Location
    Reigate and Bath, UK.
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    Thanks MatZen, you solved my problem!

    I changed

    Code:
    one:
      story_id: 1
    
    two:
      story_id: 1
    to

    Code:
    one:
      story: one
    
    two:
      story: one
    and the it all worked! I looked back in the book and I must have just skimmed over the bit where it got changed, feel a bit stupid now.

    Thanks again

  6. #6
    SitePoint Member
    Join Date
    May 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm having this same problem, but my 'votes.yml' file already had the correct settings. Any thoughts?

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

    The same exact Failures at the same exact page in the book?

  8. #8
    SitePoint Member
    Join Date
    May 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I'm getting the exact same failures that Laura K described earlier in this post, yet my votes.yml file is set correctly. I've double checked my code and it is all exactly what the same as what is in the book.

  9. #9
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,169
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Sometimes it good to check the code in the archive files as it may contain corrections. For example, from the errata page http://www.sitepoint.com/books/rails2/errata.php
    # p.241, 2nd & 4th paragraph

    All references to the latest_votes method should be changed to the latest method.
    Is that it?

  10. #10
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also - if you get unexpected errors in a process involving YAML files it is ALWAYS worth checking the yml file to make sure you haven't replaced any spaces with tabs.

    In YAML a tab has a different behaviour to a space. Therefore you have to be very careful that you don't accidentally replace spaces with tabs as this will alter the behaviour of the YAML. Be aware that some text editors will do this automatically, so you need to make sure you use a text editor that is YAML aware, or one where you can turn off the automatic substitution of spaces for tabs.

    The result of a tab being substituted for a space is often unexpected and difficult to track down errors.

    In my opinion, this white space character behaviour makes YAML the mark up language from hell. Anyone who designs a coding format that gives different meanings to different white space characters is mad and should be slapped about the face with a damp fish.

  11. #11
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I'm experience the same test_should_return_highest_vote_id_first failure, and I have checked that my yml files are correct - any other pointers?

    I'm also getting ActionView::MissingTemplate: Missing template votes/create.erb in view path app/views errors in the votes controller test.

    Any help would be appreciated

  12. #12
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    These are the errors I'm getting

    Code:
      1) Error:
    test_should_accept_vote(VotesControllerTest):
    ActionView::MissingTemplate: Missing template votes/create.erb in view path app/views
        /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-2.3.3/lib/action_view/paths.rb:66:in `find_template'
        /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-2.3.3/lib/action_controller/base.rb:1385:in `default_template'
        /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-2.3.3/lib/action_controller/base.rb:901:in `render'
        /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:51:in `block in render_with_benchmark'
        /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:17:in `block in ms'
    
      2) Error:
    test_should_redirect_after_vote_with_http_post(VotesControllerTest):
    ActionView::MissingTemplate: Missing template votes/create.erb in view path app/views
        /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-2.3.3/lib/action_view/paths.rb:66:in `find_template'
        /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-2.3.3/lib/action_controller/base.rb:1385:in `default_template'
        /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-2.3.3/lib/action_controller/base.rb:901:in `render'
        /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:51:in `block in render_with_benchmark'
        /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-2.3.3/lib/active_support/core_ext/benchmark.rb:17:in `block in ms'

  13. #13
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My missing template problem was due to missing this out from votes_controller:

    Code:
     respond_to do |format|
          format.html { redirect_to @story }
          format.js
        end

  14. #14
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,

    when i run the tests it appears that the id that gets assigned to vote 'one' is higher than the id assigned to vote 'two'?

    i added the debugging line to story_test.rb below:
    Code Ruby:
      def test_should_return_highest_vote_id_first
        puts "one id = #{votes(:one).id}, two id = #{votes(:two).id}"
        assert_equal votes(:two), stories(:one).votes.latest.first
      end

    and this is the output i get:
    Code:
    F:\InstantRails\rails_apps\shovell2>rake test:units
    (in F:/InstantRails/rails_apps/shovell2)
    F:/InstantRails/ruby/bin/ruby.exe -I"lib;test" "F:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/
    lib/rake/rake_test_loader.rb" "test/unit/helpers/stories_helper_test.rb" "test/unit/helpers/vote_helper
    _test.rb" "test/unit/story_test.rb" "test/unit/vote_test.rb"
    Loaded suite F:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader
    Started
    .....one id = 980190962, two id = 298486374
    F.
    Finished in 0.938 seconds.
    
      1) Failure:
    test_should_return_highest_vote_id_first(StoryTest) [/test/unit/story_test.rb:25]:
    <#<Vote id: 298486374, story_id: 980190962, created_at: "2009-12-13 05:48:37", updated_at: "2009-12-13
    05:48:37">> expected but was
    <#<Vote id: 980190962, story_id: 980190962, created_at: "2009-12-13 05:48:37", updated_at: "2009-12-13
    05:48:37">>.
    
    7 tests, 7 assertions, 1 failures, 0 errors
    rake aborted!
    Command failed with status (1): [F:/InstantRails/ruby/bin/ruby.exe -I"lib;t...]
    
    (See full trace by running task with --trace)
    am i doing something wrong?

    thanks in advance.

  15. #15
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Richard,

    I was getting this error too. For some reason the id on the votes is being generated oddly so that :two is no longer greater than :one. I fixed it by setting an id on votes :one and :two my self. Like this:
    Code:
    one:
      story: one
      id: 1
    
    two:
      story: one
      id: 2
    I don't know if this is a valid fix but the program its self seems to be running fine.

  16. #16
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was having errors on test_should_have_a_vote_association and test_should_return_highest_vote_id_first, in both cases it seemed to be that the votes were reversed somehow when returned from the .votes method. Hersha's suggestion seems to have fixed it, I hope it's valid. Maybe something to do with varying versions of Rails handling the YAML differently?

    Code:
      1) Failure:
    test_should_have_a_votes_association(VoteTest) [/test/unit/vote_test.rb:8]:
    <[#<Vote id: 980190962, story_id: 980190962, created_at: "2010-01-03 19:53:44", updated_at: "2010-01-03 19:53:44">,
     #<Vote id: 298486374, story_id: 980190962, created_at: "2010-01-03 19:53:44", updated_at: "2010-01-03 19:53:44">]> expected but was
    <[#<Vote id: 298486374, story_id: 980190962, created_at: "2010-01-03 19:53:44", updated_at: "2010-01-03 19:53:44">, #<Vote id: 980190962, story_id: 980190962, created_at: "2010-01-03 19:53:44", updated_at: "2010-01-03 19:53:44">]>.
    
      2) Failure:
    test_should_return_highest_vote_id_first(VoteTest) [/test/unit/vote_test.rb:12]:
    <#<Vote id: 298486374, story_id: 980190962, created_at: "2010-01-03 19:53:44", updated_at: "2010-01-03 19:53:44">> expected but was
    <#<Vote id: 980190962, story_id: 980190962, created_at: "2010-01-03 19:53:44", updated_at: "2010-01-03 19:53:44">>.

  17. #17
    Team ********* Louis Simoneau's Avatar
    Join Date
    Sep 2009
    Location
    Melbourne, Australia
    Posts
    228
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That's a legitimate fix...the change in the way rails generates IDs for fixtures happened somewhere in the 2.3 branch I think, after the book was released. However, I would recommend that if you wanted to do something like this in a real app you should base your methods on "created_at" rather than "id" to be sure you were dealing with the "latest" votes or stories.

  18. #18
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    Thank you Hersha and Louis.

    (I have sent a mail to Matt (Managing Editor) with a comment on the necessity of updating the download-able PDF version of the book two days ago, but I think now, that this forum is a better place for the issue.)
    I suggested to update the download-able PDF version of the book every time (or monthly?) a typo/error is found and corrected.

    Meantime I have some new comments on the errata management:

    • This error (pg 242) should surface in the errata page of the book. Every new reader (getting the branch 2.3) is going to catch it. ;-)


    • The errata file contains:
      # p.102, 1st code listing
      The SQL code in the book is correct, the code in the code archive is incorrect.
      Would not be better for the (future) readers to have an updated code archive?


    • Can we have the errata information ordered by page numbers?

  19. #19
    SitePoint Wizard jax502's Avatar
    Join Date
    Jul 2003
    Location
    Jacksonville, FL
    Posts
    1,031
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just wanted to update since I just came across this today. I'm on Rails 2.3.5 at the moment. The correction mentioned on this book's errata page is still wrong and will still cause some failures. The errata says

    Code:
    one: 
      story: one
      id: 1
    two: 
      story: two
      id: two
    I changed the two (in bold red) to "one" and it worked. We need an errata for the errata page :-P
    jax502

  20. #20
    Team ********* Louis Simoneau's Avatar
    Join Date
    Sep 2009
    Location
    Melbourne, Australia
    Posts
    228
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    We need an errata for the errata page :-P
    Well, that's embarrassing. Fixed now. Sorry folks.


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
  •