0

書籍「アジャイルWeb開発とレール」では、テストユニットケースの作成方法を教えています。

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

assert_equalステートメントに関して、「must bethan...」文字列の前後に角かっこが必要なのはなぜですか。ここでは変数の型が関係してくると思いますが、その理由を明確にする必要があります。

ありがとう。

4

1 に答える 1

1

model.errors[:field]エラーが 1 つしかない場合でも、常に文字列の配列を返します。

アサートがなしで行われた場合[ ]、文字列を配列と比較するため、常に false になります。

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
于 2012-11-04T20:13:42.090 に答える