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

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?!

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']:
&lt;#&lt;Vote id: 996332877, story_id: 1, created_at: "2008-07-11 13:25:45", updated_at: "2008-07-11 13:25:45"&gt;&gt; expected but was
&lt;nil&gt;.

  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']:
&lt;#&lt;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"&gt;&gt; expected but was
&lt;nil&gt;.

7 tests, 7 assertions, 3 failures, 0 errors
rake aborted!
Command failed with status (1): [/System/Library/Frameworks/Ruby.framework/...]

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).

story.rb

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

class Vote < ActiveRecord::Base
  belongs_to :story
end

story_test.rb

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

require 'test_helper'

class VoteTest < ActiveSupport::TestCase
  
  def test_story_association
    assert_equal stories(:one), votes(:one).story
  end
  
end

thanks!

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’

Thanks MatZen, you solved my problem!

I changed

one:
  story_id: 1

two:
  story_id: 1

to

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 :smiley:

I’m having this same problem, but my ‘votes.yml’ file already had the correct settings. Any thoughts?

Hi Spartacus3xxii, welcome to the forums.

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

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.

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?

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.

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

These are the errors I’m getting


  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'


My missing template problem was due to missing this out from votes_controller:

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

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:


  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:

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.

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:

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.

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?

  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">>.

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.

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. :wink:
  • 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?

[LIST]Can we have the errata information ordered by page numbers?
[/LIST]

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

one: 
  story: one
  id: 1
two: 
  story: [B][COLOR="Red"]two[/COLOR][/B]
  id: two

I changed the two (in bold red) to “one” and it worked. We need an errata for the errata page :stuck_out_tongue:

We need an errata for the errata page :stuck_out_tongue:

Well, that’s embarrassing. Fixed now. Sorry folks.