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?
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.
-
Error: test_default_case_values(ProductTest): NoMethodError: undefined method
has_case' for #<Product:0x8c65b60> test/unit/product_test.rb:53:intest_default_case_values’ -
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:intest_if_has_case_must_have_case_quantity’ -
Error: test_image_url(ProductTest): NoMethodError: undefined method
case_quantity' for #<Product:0x8c606d0> test/unit/product_test.rb:36:intest_image_url’ test/unit/product_test.rb:34:in `each’test/unit/product_test.rb:34:in `test_image_url’
-
Error: test_invalid_with_empty_attributes(ProductTest): NoMethodError: undefined method
case_quantity' for #<Product:0x8c5db20> test/unit/product_test.rb:10:intest_invalid_with_empty_attributes’ -
Error: test_positive_price(ProductTest): NoMethodError: undefined method
case_quantity' for #<Product:0x8c5af10> test/unit/product_test.rb:20:intest_positive_price’ -
Error: test_unique_title(ProductTest): NoMethodError: undefined method
case_quantity' for #<Product:0x8c57988> test/unit/product_test.rb:47:intest_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