現在、Rails の第 4 版 (Rails 3.2+) を使用したアジャイル開発を進めています。「製品」モデルの単体テストを実行しようとしています:
require 'test_helper'
class ProductTest < ActiveSupport::TestCase
test "product attributes must not be empty" do
product = Product.new
assert product.invalid?
assert product.errors[:title].any?
assert product.errors[:description].any?
assert product.errors[:price].any?
assert product.errors[:image_url].any?
end
end
これはこの本に書かれていることの一言一句であり、そうでないと言う正誤表はありません。私が実行すると:
rake test:units
私は以下を返します:
Run options:
# Running tests:
F
Finished tests in 0.079901s, 12.5155 tests/s, 25.0310 assertions/s.
1) Failure:
test_product_attributes_must_not_be_empty(ProductTest) [/Users/robertquinn/rails_work/depot/test/unit/product_test.rb:7]:
Failed assertion, no message given.
1 tests, 2 assertions, 1 failures, 0 errors, 0 skips
rake aborted!
Command failed with status (1): [/Users/robertquinn/.rvm/rubies/ruby-1.9.3-...]
Tasks: TOP => test:units
(See full trace by running task with --trace)
Robert-Quinns-MacBook-Pro:depot robertquinn$
私の製品モデルの検証は次のとおりです。
class Product < ActiveRecord::Base
attr_accessible :description, :image_url, :price, :title
validates :description, :image_url, :price, 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)$}i,
message: 'must be a URL for GIF, JPG or PNG image.'
}
end
このレーキが中止された理由がわかりません。テストにより、無効な空の「製品」オブジェクトが作成され、すべての属性にエラーが発生するはずです。ただし、「:title」属性に対する最初のアサーションをヒットすると、rake が中止されたようです。私はここでまったく無知です。あらゆるご意見をお待ちしております。