4

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

耙式测试:单位

 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
4

2 回答 2

10

应该:

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 》一书中一样。

于 2012-05-01T17:55:16.623 回答
0

对这个命令使用外部类

include Test::Unit::Assertions

require 'rubygems'
require 'selenium-webdriver'
gem "test-unit"
require 'test/unit'

include Test::Unit::Assertions

class Transaction

         def createassert()   

          assert(driver.find_element(:id, "amount").text.include?("$244.00")," amount not valid")

         end

class Evaluate

tra=Transaction.new
tra.createassert()

end
于 2013-01-23T16:06:25.443 回答