In the book Agile Web Development with Rails, they're teaching how to write test unit cases:
test "product price must be positive" do
product = Product.new(title: "By Book Title",
description: "yyy",
image_url: "zzz.jpg")
product.price = -1
assert product.invalid?
assert_equal ["must be greater than or equal to 0.01"], product.errors[:price]
end
Concerning the assert_equal statement, why are brackets needed around the "must be greater than..." string. I'm assuming variable types are coming into play here, but need some clarification on why.
Thanks.
model.errors[:field]
always returns an array of strings, even if there's only a single error.
If the assert was done without the [
]
it would always be false because it would be comparing a string to an array.
assert_equal "must be greater than or equal to 0.01", ["must be greater than or equal to 0.01"]
=> false
assert_equal ["must be greater than or equal to 0.01"], ["must be greater than or equal to 0.01"]
=> true