small medium large xlarge

Monkey self portrait straight_pragsmall
11 Nov 2010, 13:36
Max Evans (11 posts)

I guess since this is a book about Rails newbie questions are pretty standard. When I run the test in Iteration B2 I get an error and a dump of the call stack. At least that’s what it looks like to me. I enter the test like it says to do in the book like so (as it says to do in the book).

require ‘test_helper’

class ProductTest < ActiveSupport::TestCase # replace this with your real tests test “the truth” do assert true end

test “product attributes must not be empty” do product = Product.new assert product.invalid? assert product.errors[:title].any? assert product.errors[:descriptions].any? assert product.errors[:price].any? assert product.errors[:image_url].any? end end

When I run the test:units with the file like the above I get the following.

(in /Users/user/Documents/Projects/MyRuby/Rails/depot2) Loaded suite /opt/local/lib/ruby1.9/1.9.1/rake/rake_test_loader Started F. Finished in 0.099572 seconds.

1) Failure: test_product_attributes_must_not_be_empty(ProductTest) [test/unit/product_test.rb:13]: Failed assertion, no message given.

2 tests, 4 assertions, 1 failures, 0 errors, 0 skips

Test run options: –seed 58540 rake aborted! Command failed with status (1): [/opt/local/bin/ruby1.9 -I”lib:test” “/opt/…] /opt/local/lib/ruby1.9/1.9.1/rake.rb:993:in block in sh' /opt/local/lib/ruby1.9/1.9.1/rake.rb:1008:in call’ /opt/local/lib/ruby1.9/1.9.1/rake.rb:1008:in sh' /opt/local/lib/ruby1.9/1.9.1/rake.rb:1092:in sh’ /opt/local/lib/ruby1.9/1.9.1/rake.rb:1027:in ruby' /opt/local/lib/ruby1.9/1.9.1/rake.rb:1092:in ruby’ /opt/local/lib/ruby1.9/gems/1.9.1/gems/railties-3.0.1/lib/rails/test_unit/testing.rake:26:in block (2 levels) in define' /opt/local/lib/ruby1.9/1.9.1/rake.rb:1110:in verbose’ /opt/local/lib/ruby1.9/gems/1.9.1/gems/railties-3.0.1/lib/rails/test_unit/testing.rake:11:in block in define' /opt/local/lib/ruby1.9/1.9.1/rake.rb:634:in call’ /opt/local/lib/ruby1.9/1.9.1/rake.rb:634:in block in execute' /opt/local/lib/ruby1.9/1.9.1/rake.rb:629:in each’ /opt/local/lib/ruby1.9/1.9.1/rake.rb:629:in execute' /opt/local/lib/ruby1.9/1.9.1/rake.rb:595:in block in invoke_with_call_chain’ /opt/local/lib/ruby1.9/1.9.1/monitor.rb:201:in mon_synchronize' /opt/local/lib/ruby1.9/1.9.1/rake.rb:588:in invoke_with_call_chain’ /opt/local/lib/ruby1.9/1.9.1/rake.rb:581:in invoke' /opt/local/lib/ruby1.9/1.9.1/rake.rb:2041:in invoke_task’ /opt/local/lib/ruby1.9/1.9.1/rake.rb:2019:in block (2 levels) in top_level' /opt/local/lib/ruby1.9/1.9.1/rake.rb:2019:in each’ /opt/local/lib/ruby1.9/1.9.1/rake.rb:2019:in block in top_level' /opt/local/lib/ruby1.9/1.9.1/rake.rb:2058:in standard_exception_handling’ /opt/local/lib/ruby1.9/1.9.1/rake.rb:2013:in top_level' /opt/local/lib/ruby1.9/1.9.1/rake.rb:1992:in run’ /opt/local/bin/rake1.9:31:in `'

If I comment out the bottom three lines, like following, it runs OK.

require ‘test_helper’

class ProductTest < ActiveSupport::TestCase # replace this with your real tests test “the truth” do assert true end

test “product attributes must not be empty” do product = Product.new assert product.invalid? assert product.errors[:title].any? #assert product.errors[:descriptions].any? #assert product.errors[:price].any? #assert product.errors[:image_url].any? end end

Since I’m so new to Ruby and Rails I really can’t see what is going on here. The book says that it should run OK.

I’m on a MacBook Pro. I installed Ruby 1.9.1 using MacPorts. This means that I call rake with the command rake1.9. I keep thinking that that might be part of the problem, but I don’t see why part of the file would work in that case. So that might not be it.

Any help would be appreciated.

Monkey self portrait straight_pragsmall
11 Nov 2010, 14:59
Max Evans (11 posts)

Never mind.

I removed the test that didn’t work and moved onto the next example, which worked. Then I decided that I would try something else with the test that didn’t work, and after typing it in again it worked. As far as I can tell it is the same but now it works.

Generic-user-small
22 Feb 2011, 15:54
Giampiero De Ciantis (2 posts)

I am experiencing a similar issue and I cannot fix it. The rake task constantly throws errors on product.invalid? I even tried to remove a bunch of code to narrow it down.

I am also using Ruby1.9 installed using MacPorts.

  test "product price must be positive" do
    product = Product.new(:title        => "My Book Title",
                          :description  => "yyy",
                          :image_url    => "zzz.jpg")
    product.price = -1
    assert product.invalid?
#    assert_equal "must be greater than or equal to 0.01", product.errors[:price].join('; ')
    
#    product.price = 0
#    assert product.invalid?
#    assert_equal "must be greater than or equal to 0.01", product.errors[:price].join('; ')
    
#    product.price = 1
#    assert product.valid?
  end
Generic-user-small
23 Feb 2011, 13:58
Giampiero De Ciantis (2 posts)

Ok, tracked it down. The first error that I was experiencing had to do with the numericality validation. I either fat fingered it or there is an error in the text. The second issue was with a space between Product.new and the opening parenthesis.

You must be logged in to comment