On Aug 19, 2010, at 12:02 PM, Madison Kelly wrote:
> Hello --
>
> Thanks in advance for putting up with a newbie question:)
>
> I added two columns to my Products model. The db migration completed without
> error, I added the items to the views, and I tested the expected behavior on
> the site itself. The properties can be set, stored, and is displayed later. I
> also put some validation in the model, and the errors display perfectly. So
> everything is working just as I would expect.
>
> Where I ran into trouble is when I ran the unit tests I have for the products
> model. I'm getting errors that say the properties are an "undefined method".
> Even for test cases that don't refer to these properties, they are returning
> the same error. I assume this is from referencing the model. Why does
> everything work correctly on the site, but the test cases don't seem to
> recognize the new properties I've added to the model?
Don't you need to setup the test database?
rake db:test:clone # Recreate the test database from the current
environment's database schema
rake db:test:clone_structure # Recreate the test databases from the
development structure
rake db:test:load # Recreate the test database from the current
schema.rb
rake db:test:prepare # Check for pending migrations and load the test
schema
rake db:test:purge # Empty the test database
>
> Thanks!
> Madison
>
>
> Additional information below:
>
> (note: two new columns are has_case and case_quantity)
>
> Error:
> C:\webapp\depot>ruby -I test test/unit/product_test.rb
> Loaded suite test/unit/product_test
> Started
> EEEEEE
> Finished in 0.218401 seconds.
> 1) Error:
> test_default_case_values(ProductTest):
> NoMethodError: undefined method `has_case' for #<Product:0x8c65b60>
> test/unit/product_test.rb:53:in `test_default_case_values'
> 2) Error:
> test_if_has_case_must_have_case_quantity(ProductTest):
> ActiveRecord::UnknownAttributeError: unknown attribute: has_case
> test/unit/product_test.rb:58:in `new'
> test/unit/product_test.rb:58:in `test_if_has_case_must_have_case_quantity'
> 3) Error:
> test_image_url(ProductTest):
> NoMethodError: undefined method `case_quantity' for #<Product:0x8c606d0>
> test/unit/product_test.rb:36:in `test_image_url'
> test/unit/product_test.rb:34:in `each'
> test/unit/product_test.rb:34:in `test_image_url'
> 4) Error:
> test_invalid_with_empty_attributes(ProductTest):
> NoMethodError: undefined method `case_quantity' for #<Product:0x8c5db20>
> test/unit/product_test.rb:10:in `test_invalid_with_empty_attributes'
> 5) Error:
> test_positive_price(ProductTest):
> NoMethodError: undefined method `case_quantity' for #<Product:0x8c5af10>
> test/unit/product_test.rb:20:in `test_positive_price'
> 6) Error:
> test_unique_title(ProductTest):
> NoMethodError: undefined method `case_quantity' for #<Product:0x8c57988>
> test/unit/product_test.rb:47:in `test_unique_title'
> 6 tests, 0 assertions, 0 failures, 6 errors
> C:\webapp\depot>
>
>
> Model:
> class Product < ActiveRecord::Base
> has_many :orders, :through=> :line_items
> has_many :line_items
> validates_presence_of :title, :description, :image_url, :price
> validates_numericality_of :price, :case_quantity
> validate :price_must_be_at_least_a_cent, :if_case_must_have_quantity,
> :if_quantity_must_have_case
> validates_uniqueness_of :title
> validates_format_of :image_url,
> :with => %r{\.(gif|jpg|png)$}i,
> :message => 'must be a URL for GIF, JPG ' + 'or PNG
> image.'
> protected
> def price_must_be_at_least_a_cent
> errors.add(:price, 'should be at least 0.01') if price.nil? || price <
> 0.01
> end
> def if_case_must_have_quantity
> if (has_case == true) && (case_quantity < 1)
> errors.add(:case_quantity, ': must set case quantity if this product
> can be sold in cases')
> end
> end
> def if_quantity_must_have_case
> if (has_case == false) && (case_quantity > 0)
> errors.add(:case_quantity, ': must be 0 if product is not sold in
> cases')
> end
> end
> def self.find_products_for_sale
> find(:all, :order => "title")
> end
> end
>
>
> DB migration:
> class AddCaseAndQuantityToProducts < ActiveRecord::Migration
> def self.up
> add_column :products, :has_case, :boolean, :default => false
> add_column :products, :case_quantity, :integer, :default => 0
> end
> def self.down
> remove_column :products, :has_case
> remove_column :products, :case_quantity
> end
> end
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.