我正在关注敏捷网站开发。我正处于本书的单元测试阶段。我跟着它发球了。但是当我运行rake时,我遇到以下错误:测试单位
rake aborted!
ArgumentError: wrong number of arguments (1 for 2)
/Users/ray/Desktop/work/depot/test/models/product_test.rb:15:in `test'
/Users/ray/Desktop/work/depot/test/models/product_test.rb:15:in `<top (required)>'
/Users/ray/.rvm/gems/ruby-2.2.0/gems/railties- 4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `require'
/Users/ray/.rvm/gems/ruby-2.2.0/gems/railties- 4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/Users/ray/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/Users/ray/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/Users/ray/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/Users/ray/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
Tasks: TOP => test:units
(See full trace by running task with --trace)
我的test / models / product_test.rb如下所示:
require 'test_helper'
class ProductTest < ActiveSupport::TestCase
fixtures :products
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
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
def new_product(image_url)
Product.new(title: "My Book Title",
description: "yyy",
price: 1,
image_url: image_url)
end
test "image url" do
ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg
http://a.b.c/x/y/z/fred.gif }
bad = %w{ fred.doc fred.gif/more fred.gif.more }
ok.each do |name|
assert new_product(name).valid?, "#{name} shouldn't be invalid"
end
bad.each do |name|
assert new_product(name).invalid?, "#{name} shouldn't be valid"
end
end
test "product is not valid without a unique title" do
product = Product.new(title: products(:ruby).title,
description: "yyy",
price: 1,
image_url: "fred.gif")
assert !product.save
assert_equal "has already been taken", product.errors[:title].join('; ')
end
test "product is not valid without a unique title - i18n" do
product = Product.new( title: products(:ruby).title,
description: "yyy",
price: 1,
image_url: "fred.gif")
assert !product.save
assert_equal I18n.translate('activerecord.errors.messages.taken'),
product.errors[:title].join('; ')
end
我的test / fixtures / products.yml看起来像这样
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
title: MyString
description: MyText
image_url: MyString
price: 9.99
two:
title: MyString
description: MyText
image_url: MyString
price: 9.99
ruby:
title: Programming Ruby 1.9
description:
Ruby is the fastest growing and most exciting dynamic
language out there. If you need to get working programs
delivered fast, you should add Ruby to your toolbox.
price: 49.50
image_url: ruby.png
我真的很擅长朝着正确的方向点头,或者建议会非常感激。感谢
修改
继承我的app / models / 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 image.'
}
end
答案 0 :(得分:0)
测试文件 - test/models/product_test.rb
- 语法错误;这就是导致错误信息。第12行和第12行有两个end
语句。该文件中的13个。删除其中一个并将其放在文件的末尾;这将解决这个问题。
由于第12行的额外end
,测试文件在该点被认为是完整的,并且下一行在不同的上下文中被解释导致问题。