私はRailsを学んでおり、単体テストを勉強しています。私が使用している本の 1 つは、明らかに間違って書かれた例を示しています。
これは検証です:
class Product < ActiveRecord::Base
attr_accessible :description, :image_url, :price, :title
#VALIDATION PROCESS
validates :title, :description, :image_url, :presence => true, :length => {:minimum => 10}
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
validates :title, :uniqueness => true
validates :image_url, :format => {
:with => %r{\.(gif|jpg|png)$}i,
:message => 'Must be a URL for GIF,PNG or JPG image!'
}
end
そして、これはテストです:
require 'test_helper'
class ProductTest < ActiveSupport::TestCase
test "product price must be positive" do
product = Product.new(:title => "My 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].join('; ' )
product.price = 0
assert product.invalid?
assert_equal "must be greater than or equal to 0.01" ,
product.errors[:price].join('; ' )
product.price = 1
assert product.valid?
end
end
を使用してコマンド ラインでテストを実行するとrake test:units
、最後のアサーションでエラーが発生します。
product.price = 1
assert product.valid?
それは言いFailured assertion, no message given
ます。
奇妙なことは、本自体がこの特定の主張が真実であると述べているため、テスト中に何も起こらないはずです.
それで、どうしたの?コードが間違っているのでしょうか、それとも何か間違ったことをしているのですか、それとも正しくて混乱しているだけですか?