我刚刚进入Rails测试,我使用内置测试“语言”而不是Rspec。对于我的生活,我无法弄清楚为什么这仍然失败。
test "product title is at least 10 chars long" do
product = Product.new(description: 'yyy',
image_url: 'zzz.jpg',
price: 1)
product.title = 'testitout'
assert product.invalid?
assert_equal ['title must be at least 10 chars long'], product.errors[:title]
product.title = 'testitoutt'
assert product.valid?
end
这是Product.rb
class Product < ActiveRecord::Base
validates :title, :description, :image_url, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)\Z}i,
message: 'must be a URL for GIF, JPG, or PNG'
}
validates :title, length: { minimum: 10 }
end
这是我的终端告诉我的。
1) Failure:
ProductTest#test_product_title_is_at_least_10_chars_long [/test/models/product_test.rb:62]:
--- expected
+++ actual
@@ -1 +1 @@
-["Title is too short (minimum is 10 characters)"]
+[]
5 runs, 23 assertions, 1 failures, 0 errors, 0 skips