06 Nov 2013, 22:13
Me_pragsmall

Colin Williams (9 posts)

1:[No Name] X Add unit tests which add unique products and duplicate products. Note that you will need to modify the fixture to refer to products and carts by name, for example product: ruby.

http://pragprog.com/wikis/wiki/Pt-E-2

I posted my test code below. For some reason I have to comment out the total price method. Not sure what is causing the error.

 [discord@deadia depot]$ rake test
 Run options: --seed 30016
 
 # Running tests:
 
 ...E..........................
 
 Finished tests in 0.936826s, 32.0230 tests/s, 82.1924 assertions/s.
 
 1) Error:
 CartTest#test_test_adding_unique_products:
 NoMethodError: undefined method `price' for nil:NilClass
 app/models/line_item.rb:5:in `total_price'
 app/models/cart.rb:13:in `block in total_price'
 app/models/cart.rb:13:in `total_price'
 test/models/cart_test.rb:39:in `block in <class:CartTest>'
 
 30 tests, 77 assertions, 0 failures, 1 errors, 0 skips
 
 
 
 
 
 test "test adding unique products" do
   product = Product.new(title:"Unique Title 1", description:"this is a unique book", image_url: "rrr.jpg",price: "23.23")
   product2 = Product.new(title:"Unique Title 2", description:"this is a unique book", image_url: "yyy.jpg",price: "23.23")
   cart = Cart.new
   cart.add_product(product)
   cart.add_product(product2)
   assert cart.save
   assert cart.line_items.size == 2
   #assert cart.total_price == "46.46"
   product3 =  Product.new(title:"Unique Title 13", description:"this is a unique book with a duplicate image_url", image_url: "rrr.jpg", price: "23.23")
   assert product3.invalid?
 end

I’m also having trouble with getting an invalid product for product3, even though I added validates :image_url, uniqueness: true

to the product class. Why isn’t product3 invalid? It uses the same image_url as product.

08 Nov 2013, 19:35
Me_pragsmall

Colin Williams (9 posts)

Silly me, I was comparing total_price to a string…. I seem to pass the unique product test now, including product3.invalid

  You must be logged in to comment