Rails Rails 5 敏捷开发这本书上的例子测试出错。高手来看看。

javaruby · 2022年10月29日 · 最后由 javaruby 回复于 2022年11月10日 · 565 次阅读

Product 的定义

class Product < ApplicationRecord
  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

ProductTest 的定义

require 'test_helper'

class ProductTest < ActiveSupport::TestCase
  # test "the truth" do
  #   assert true
  # end
  test "product attribute 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

  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 product.errors[:price],["must be greater than or equal to 0.01"]

    product.price = 0
    assert product.invalid?
    assert_equal product.errors[:price],["must be greater than or equal to 0.01"]

    product.price = 0.01
    assert product.valid?
  end

  def new_product(image_url)
    Product.new(title: "my book title",
                description: "yyy",
                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/mor 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

end

运行 bin/rails test:models 后报错,有时候是报错 test_product_price_must_be_positive,有时候是报错 test_image_url

Failure:
ProductTest#test_product_price_must_be_positive [/Users/sun/Desktop/rubywork/depot/test/models/product_test.rb:29]:
Expected false to be truthy.
Failure:
ProductTest#test_image_url [/Users/sun/Desktop/rubywork/depot/test/models/product_test.rb:44]:
fred.GIF shouldn't be invalid```

注意是小括号不是中括号。

with: %r{\.(gif|jpg|png)\Z}i

你可以通过指定一个文件与行号单独跑一个测试 rails test test/models/product_test.rb:16

如果要看哪里错了,你可以 debug 一下.

不知道为什么,但这个正则错了,我对正则不熟,无法告诉你为什么,改成这样可以

with: %r{[gif|jpg|png]\z}i,
Rei 回复

中扣号也可以表达正则表达式的

为什么我的代码都是按书上来的,也仔细检查了,可是 rails test 的时候一会儿报这个错误,一会儿报那个错误

javaruby 回复

小括号和中括号的正则含义可不一样。

运行 3 次 rails test ,报的错误是不一样的,头大啊。

  depot rails test
Running via Spring preloader in process 21252
Run options: --seed 5941

# Running:

F

Failure:
ProductsControllerTest#test_should_update_product [/Users/sun/Desktop/rubywork/depot/test/controllers/products_controller_test.rb:44]:
Expected response to be a <3XX: redirect>, but was a <422: Unprocessable Entity>


Traceback (most recent call last):
    27: from -e:1:in `<main>'
    26: from /Users/sun/.rvm/rubies/ruby-2.6.0/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    25: from /Users/sun/.rvm/rubies/ruby-2.6.0/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    24: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/spring-2.1.1/lib/spring/application/boot.rb:19:in `<top (required)>'
    23: from /Users/sun/.rvm/gems/[email protected]/gems/spring-2.1.1/lib/spring/application.rb:139:in `run'
    22: from /Users/sun/.rvm/gems/[email protected]/gems/spring-2.1.1/lib/spring/application.rb:139:in `loop'
    21: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/spring-2.1.1/lib/spring/application.rb:145:in `block in run'
    20: from /Users/sun/.rvm/gems/[email protected]/gems/spring-2.1.1/lib/spring/application.rb:180:in `serve'
    19: from /Users/sun/.rvm/gems/[email protected]/gems/spring-2.1.1/lib/spring/application.rb:180:in `fork'
    18: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:83:in `block in autorun'
    17: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:159:in `run'
    16: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:182:in `__run'
    15: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:182:in `map'
    14: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:182:in `block in __run'
    13: from /Users/sun/.rvm/gems/[email protected]/gems/railties-5.0.7.2/lib/rails/test_unit/line_filtering.rb:9:in `run'
    12: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:350:in `run'
    11: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:378:in `with_info_handler'
    10: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:391:in `on_signal'
     9: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:351:in `block in run'
     8: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:351:in `each'
     7: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:352:in `block (2 levels) in run'
     6: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:365:in `run_one_method'
     5: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:890:in `record'
     4: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:890:in `each'
     3: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:891:in `block in record'
     2: from /Users/sun/.rvm/gems/[email protected]/gems/railties-5.0.7.2/lib/rails/test_unit/reporter.rb:23:in `record'
     1: from /Users/sun/.rvm/gems/[email protected]/gems/railties-5.0.7.2/lib/rails/test_unit/reporter.rb:70:in `format_rerun_snippet'
/Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/railties-5.0.7.2/lib/rails/test_unit/reporter.rb:70:in `method': undefined method `test_should_update_product' for class `Minitest::Result' (NameError)
  depot rails test
Running via Spring preloader in process 21268
Run options: --seed 59470

# Running:

F

Failure:
ProductTest#test_product_price_must_be_positive [/Users/sun/Desktop/rubywork/depot/test/models/product_test.rb:29]:
Expected false to be truthy.


Traceback (most recent call last):
    27: from -e:1:in `<main>'
    26: from /Users/sun/.rvm/rubies/ruby-2.6.0/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    25: from /Users/sun/.rvm/rubies/ruby-2.6.0/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    24: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/spring-2.1.1/lib/spring/application/boot.rb:19:in `<top (required)>'
    23: from /Users/sun/.rvm/gems/[email protected]/gems/spring-2.1.1/lib/spring/application.rb:139:in `run'
    22: from /Users/sun/.rvm/gems/[email protected]/gems/spring-2.1.1/lib/spring/application.rb:139:in `loop'
    21: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/spring-2.1.1/lib/spring/application.rb:145:in `block in run'
    20: from /Users/sun/.rvm/gems/[email protected]/gems/spring-2.1.1/lib/spring/application.rb:180:in `serve'
    19: from /Users/sun/.rvm/gems/[email protected]/gems/spring-2.1.1/lib/spring/application.rb:180:in `fork'
    18: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:83:in `block in autorun'
    17: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:159:in `run'
    16: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:182:in `__run'
    15: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:182:in `map'
    14: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:182:in `block in __run'
    13: from /Users/sun/.rvm/gems/[email protected]/gems/railties-5.0.7.2/lib/rails/test_unit/line_filtering.rb:9:in `run'
    12: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:350:in `run'
    11: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:378:in `with_info_handler'
    10: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:391:in `on_signal'
     9: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:351:in `block in run'
     8: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:351:in `each'
     7: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:352:in `block (2 levels) in run'
     6: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:365:in `run_one_method'
     5: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:890:in `record'
     4: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:890:in `each'
     3: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:891:in `block in record'
     2: from /Users/sun/.rvm/gems/[email protected]/gems/railties-5.0.7.2/lib/rails/test_unit/reporter.rb:23:in `record'
     1: from /Users/sun/.rvm/gems/[email protected]/gems/railties-5.0.7.2/lib/rails/test_unit/reporter.rb:70:in `format_rerun_snippet'
/Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/railties-5.0.7.2/lib/rails/test_unit/reporter.rb:70:in `method': undefined method `test_product_price_must_be_positive' for class `Minitest::Result' (NameError)
  depot rails test
Running via Spring preloader in process 21274
Run options: --seed 10657

# Running:

....F

Failure:
ProductsControllerTest#test_should_create_product [/Users/sun/Desktop/rubywork/depot/test/controllers/products_controller_test.rb:25]:
"Product.count" didn't change by 1.
Expected: 3
  Actual: 2


Traceback (most recent call last):
    27: from -e:1:in `<main>'
    26: from /Users/sun/.rvm/rubies/ruby-2.6.0/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    25: from /Users/sun/.rvm/rubies/ruby-2.6.0/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    24: from /Users/sun/.rvm/gems/[email protected]/gems/spring-2.1.1/lib/spring/application/boot.rb:19:in `<top (required)>'
    23: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/spring-2.1.1/lib/spring/application.rb:139:in `run'
    22: from /Users/sun/.rvm/gems/[email protected]/gems/spring-2.1.1/lib/spring/application.rb:139:in `loop'
    21: from /Users/sun/.rvm/gems/[email protected]/gems/spring-2.1.1/lib/spring/application.rb:145:in `block in run'
    20: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/spring-2.1.1/lib/spring/application.rb:180:in `serve'
    19: from /Users/sun/.rvm/gems/[email protected]/gems/spring-2.1.1/lib/spring/application.rb:180:in `fork'
    18: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:83:in `block in autorun'
    17: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:159:in `run'
    16: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:182:in `__run'
    15: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:182:in `map'
    14: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:182:in `block in __run'
    13: from /Users/sun/.rvm/gems/[email protected]/gems/railties-5.0.7.2/lib/rails/test_unit/line_filtering.rb:9:in `run'
    12: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:350:in `run'
    11: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:378:in `with_info_handler'
    10: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:391:in `on_signal'
     9: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:351:in `block in run'
     8: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:351:in `each'
     7: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:352:in `block (2 levels) in run'
     6: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:365:in `run_one_method'
     5: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/minitest-5.16.3/lib/minitest.rb:890:in `record'
     4: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:890:in `each'
     3: from /Users/sun/.rvm/gems/[email protected]/gems/minitest-5.16.3/lib/minitest.rb:891:in `block in record'
     2: from /Users/sun/.rvm/gems/ruby-2.6.0@rails5.0.2/gems/railties-5.0.7.2/lib/rails/test_unit/reporter.rb:23:in `record'
     1: from /Users/sun/.rvm/gems/[email protected]/gems/railties-5.0.7.2/lib/rails/test_unit/reporter.rb:70:in `format_rerun_snippet'
/Users/sun/.rvm/gems/[email protected]/gems/railties-5.0.7.2/lib/rails/test_unit/reporter.rb:70:in `method': undefined method `test_should_create_product' for class `Minitest::Result' (NameError)

我安装的 rails 版本是 5.0.2,可是执行 bundle install 以后 gem 多了一个 5.0.7.2,是不是这个原因造成错误的?有高手能说一下吗?

depot gem list --local rails

*** LOCAL GEMS ***

coffee-rails (4.2.2)
jquery-rails (4.5.0)
rails (5.0.7.2, 5.0.2)
rails-dom-testing (2.0.3)
rails-html-sanitizer (1.4.3)
sass-rails (5.0.7)
sprockets-rails (3.2.2)
javaruby 回复

需要花费 debug 时间,照着书上一字不差出问题太正常了

已经解决了,是版本混乱造成的

需要 登录 后方可回复, 如果你还没有账号请 注册新账号