在ruby中从单元测试初始化​​env.rb

时间:2014-02-14 16:24:50

标签: ruby unit-testing cucumber

如何从env.rb初始化Test::Unit::TestCase。我希望能够在我的Ruby / Cucumber应用程序中对我的ruby方法进行单元测试,并且env.rb可以进行大量的设置。

简化示例:

env.rb

require 'logger'

$LOG = Logger.new(STDOUT)

HelloWorld.rb

class HelloWorld
  def say_hi
    greeting = 'Hello World'

    $LOG.info greeting

    greeting
  end
 end

TestHelloWorld.rb

require 'hello_world'

class TestHelloWorld < Test::Unit::TestCase

  def test_say_hi
    assert_equal('Hello World', HelloWorld.new.say_hi)
  end
end

正在运行TestHellowWorld为{nilClass`提供NoMethodError: undefined method info':

1 个答案:

答案 0 :(得分:0)

假设Test :: Unit脚本目录和黄瓜项目是兄弟姐妹,您可以使用relative requireenv.rb。例如:

<强> /cucumber_project/features/support/env.rb

require 'logger'
$LOG = Logger.new(STDOUT)

<强> hello_world.rb

class HelloWorld
  def say_hi
    greeting = 'Hello World'
    $LOG.info greeting
    greeting
  end
end

<强> test.rb

require_relative 'hello_world'
require_relative '../cucumber_project/features/support/env'
require 'test/unit'

class TC_HelloWorld < Test::Unit::TestCase

  def test_say_hi
    assert_equal('Hello World', HelloWorld.new.say_hi)
  end

end