small medium large xlarge

Generic-user-small
28 May 2010, 21:20
Scott Herrington (6 posts)

I am getting some pretty strange behavior running the code in Iteration B2: Unit Testing of Models.

I used the downloaded code, so don’t think I’ve made any typographical errors.

I am getting two failures:

test_product_attributes_must_not_be_empty

is not true test_product_price_must_be_positive is not true. Any ideas on what might be going wrong?
Samr_small_pragsmall
29 May 2010, 02:13
Sam Ruby (634 posts)

The errors should tell you which line in the tests that are failing. Looking at those lines you should see:

assert product.errors[:price].any?
assert product.invalid?

In both cases, they are testing the validation code on price. Now if you visit http://localhost:3000/products/new and immediately click Create Product you should see 4 errors, and the error messages should include Price must be greater than or equal to 0.01. Given what you have described, I expect that that message will NOT be included.

Now, take a look at app/models/product.rb, and verify that the following line is present:

validates :price, :numericality => {:greater_than_or_equal_to => 0.01}

I find that if I remove that line of code I get exactly the symptoms you have described.

Generic-user-small
29 May 2010, 05:23
Scott Herrington (6 posts)

Thank you for the reply. You are correct, I did in fact omit the validates :price, :numericality => {:greater_than_or_equal_to => 0.01} in the product model (product.rb).

You must be logged in to comment