product_test.rb 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #---
  2. # Excerpted from "Agile Web Development with Rails",
  3. # published by The Pragmatic Bookshelf.
  4. # Copyrights apply to this code. It may not be used to create training material,
  5. # courses, books, articles, and the like. Contact us if you are in doubt.
  6. # We make no guarantees that this code is fit for any purpose.
  7. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  8. #---
  9. #---
  10. # Excerpted from "Agile Web Development with Rails, 4rd Ed.",
  11. # published by The Pragmatic Bookshelf.
  12. # Copyrights apply to this code. It may not be used to create training material,
  13. # courses, books, articles, and the like. Contact us if you are in doubt.
  14. # We make no guarantees that this code is fit for any purpose.
  15. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  16. #---
  17. require 'test_helper'
  18. class ProductTest < ActiveSupport::TestCase
  19. test "product attributes must not be empty" do
  20. product = Product.new
  21. assert product.invalid?
  22. assert product.errors[:title].any?
  23. assert product.errors[:description].any?
  24. assert product.errors[:price].any?
  25. assert product.errors[:image_url].any?
  26. end
  27. test "product price must be positive" do
  28. product = Product.new(:title => "My Book Title",
  29. :description => "yyy",
  30. :image_url => "zzz.jpg")
  31. product.price = -1
  32. assert product.invalid?
  33. assert_equal "must be greater than or equal to 0.01",
  34. product.errors[:price].join('; ')
  35. product.price = 0
  36. assert product.invalid?
  37. assert_equal "must be greater than or equal to 0.01",
  38. product.errors[:price].join('; ')
  39. product.price = 1
  40. assert product.valid?
  41. end
  42. def new_product(image_url)
  43. Product.new(:title => "My Book Title",
  44. :description => "yyy",
  45. :price => 1,
  46. :image_url => image_url)
  47. end
  48. test "image url" do
  49. ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg
  50. http://a.b.c/x/y/z/fred.gif }
  51. bad = %w{ fred.doc fred.gif/more fred.gif.more }
  52. ok.each do |name|
  53. assert new_product(name).valid?, "#{name} shouldn't be invalid"
  54. end
  55. bad.each do |name|
  56. assert new_product(name).invalid?, "#{name} shouldn't be valid"
  57. end
  58. end
  59. test "product is not valid without a unique title" do
  60. product = Product.new(:title => products(:ruby).title,
  61. :description => "yyy",
  62. :price => 1,
  63. :image_url => "fred.gif")
  64. assert !product.save
  65. assert_equal "has already been taken", product.errors[:title].join('; ')
  66. end
  67. test "product is not valid without a unique title - i18n" do
  68. product = Product.new(:title => products(:ruby).title,
  69. :description => "yyy",
  70. :price => 1,
  71. :image_url => "fred.gif")
  72. assert !product.save
  73. assert_equal I18n.translate('activerecord.errors.messages.taken'),
  74. product.errors[:title].join('; ')
  75. end
  76. end