You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

77 rivejä
2.3 KiB

  1. require 'test_helper'
  2. class ProductTest < ActiveSupport::TestCase
  3. # Rails is already doing this! (convention over configuration!)
  4. fixtures :products
  5. # test "the truth" do
  6. # assert true
  7. # end
  8. test "product attributes must not be empty" do
  9. product = Product.new
  10. assert product.invalid?
  11. assert product.errors[:title].any?
  12. assert product.errors[:description].any?
  13. assert product.errors[:image_url].any?
  14. assert product.errors[:price].any?
  15. end
  16. test "product price be positive" do
  17. product = Product.new(title: 'My Book Title',
  18. description: 'My Description.',
  19. image_url: 'image.jpg')
  20. product.price = -1
  21. assert product.invalid?
  22. assert_equal ['must be greater than or equal to 0.01'],
  23. product.errors[:price]
  24. product.price = 0
  25. assert product.invalid?
  26. assert_equal ['must be greater than or equal to 0.01'],
  27. product.errors[:price]
  28. product.price = 1
  29. assert product.valid?
  30. end
  31. def new_product(image_url)
  32. product = Product.new(title: 'My Book Title',
  33. description: 'My Description.',
  34. image_url: image_url,
  35. price: 1)
  36. end
  37. test "image url" do
  38. ok = %w{ fred.jpg hans.png paul.Png peter.JPG
  39. http://a.b.c.d/images/foo.gif }
  40. bad =%w{ fred.doc hans.gif/more }
  41. ok.each { |name| assert new_product(name).valid? "#{name} shouldn't be invalid" }
  42. bad.each do |name|
  43. assert new_product(name).invalid? "#{name} shouldn't be valid"
  44. end
  45. end
  46. test "product is not valid without a unique title - i18n" do
  47. product = Product.new(title: products(:ruby).title,
  48. description: 'Foo.',
  49. price: 1,
  50. image_url: "foo.jpg")
  51. assert product.invalid?
  52. assert_equal [I18n.translate('errors.messages.taken')],
  53. product.errors[:title]
  54. end
  55. test "product is not valid without minimum 5 characters in title" do
  56. product = Product.new(title: 'Shrt',
  57. description: 'A short work.',
  58. price: 0.99,
  59. image_url: 'short.jpg')
  60. assert product.invalid?
  61. assert_equal ['is too short (minimum is 5 characters)'],
  62. product.errors[:title]
  63. end
  64. end