ruby-on-rails-3 – 在轨道上的ruby中的未定义方法`assert’

运行该命令后收到此错误消息

耙子测试:单位

undefined method `assert' for ProductTest:Class (NoMethodError)
    from /home/mayank/src/proj/vendor/bundle/ruby/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
    from /home/mayank/src/proj/vendor/bundle/ruby/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
    from /home/mayank/src/proj/vendor/bundle/ruby/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /home/mayank/src/proj/vendor/bundle/ruby/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
    from /home/mayank/src/proj/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:10
    from /home/mayank/src/proj/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:9:in `each'
    from /home/mayank/src/proj/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:9
    from /home/mayank/src/proj/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:4:in `select'
    from /home/mayank/src/proj/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/rake_test_loader.rb:4
rake aborted!

该应用程序的源代码如下.它在线上给出错误断言.

require 'test_helper'

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

解决方法

应该:

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[:image_url].any?
    assert product.errors[:price].any?
  end
end

就像书中的Agile Web Development with Rails一样.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...