如何从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':
答案 0 :(得分:0)
假设Test :: Unit脚本目录和黄瓜项目是兄弟姐妹,您可以使用relative require
来env.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