1

I am trying to test a single method in ruby. It is in a separate file so basically:

a.rb:

def my_method
  ...
end

in my a_spec.rb

require 'minitest/autorun'

Object.instance_eval do
  load("path_to/a.rb")

  def hello_world
    ...
  end
end

When I try to run my test, it says that my_method is a private method while I can actually call Object.hello_world outright. What gives?

Also, is there an easier way to test plain ruby methods(no classes or modules) with minitest?

4

1 回答 1

0

执行上述加载不会将a.rb的方法作为单例方法添加到 Object。相反,它将方法添加到全局命名空间。(您在 self 引用 Object 类的块内进行加载的事实是无关紧要的。)

使用上面的代码,您应该能够在测试中直接调用 *my_method* :

class MyTest <  MiniTest::Unit::TestCase

  def test_my_method
    assert my_method
  end

  def test_hello_world
    assert Object.hello_world
  end
end
于 2012-05-29T21:37:17.510 回答