新手问题 新人求助,关于 Test::Unit::TestCase 下

liuvz · December 20, 2016 · Last by michael replied at December 30, 2016 · 1623 hits
hello = "hello"
MyTest = Class.new(Test::Unit::TestCase) do 
        define_method :test_hello do
                assert(hello=="hello","啊哈哈哈")
        end
end

这段代码为什么不能运行?instance_methods(false) 可以返回正确的方法名啊 看到一句话,Test::Unit::TestCase 是通过 反射 找到该使用的方法的,跟这个有关么,反射跟 instance_methods 不一样么? 不太明白....

这是 Python 代码么?

为什么这么说?Ruby 代码啊

是这样的,我想让方法 test_hello 可以访问 hello

这段代码为什么不能运行? Ans:看看错误 traceback, 不贴错误如何诊断?

难道不是这样吗?

# File:  tc_simple_number2.rb

require_relative "simple_number"
require "test/unit"

class TestSimpleNumber < Test::Unit::TestCase

  def test_simple
    assert_equal(4, SimpleNumber.new(2).add(2) )
    assert_equal(4, SimpleNumber.new(2).multiply(2) )
  end

  def test_typecheck
    assert_raise( RuntimeError ) { SimpleNumber.new('a') }
  end

  def test_failure
    assert_equal(3, SimpleNumber.new(2).add(2), "Adding doesn't work" )
  end

end

以上代码来源于 https://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing

You need to Sign in before reply, if you don't have an account, please Sign up first.