在ruby中运行单元测试时,require失败

时间:2015-03-09 19:11:51

标签: ruby-on-rails ruby unit-testing ruby-on-rails-4

因此,当我尝试运行以下文件时,我收到此错误。当我使用ruby -I Zombifier_test.rb运行它时,它会永远卡住并挂起。我如何使这项工作?

 ruby Zombifier_test.rb

/home/.rubies/ruby-2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- Zombifier (LoadError)
    from /home/.rubies/ruby-2.1.3/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from Zombifier_test.rb:2:in `<main>'

Zombifier_test.rb文件的内容如下:

require 'test/unit'
require 'Zombifier'

class ZombifierTest< Test::Unit::TestCase

  # Called before every test method runs. Can be used
  # to set up fixture information.
  def test_zombify_upcase
     z=Zombifier.new("Hello World")

    assert_equal "HELLO WORLD BRAINS",z.zombify,"They are not equal"

    end



end

Zombifier.rb的内容如下:

class Zombifier

  def initialize(str)
    @str = str
  end

  def zombify
    "#{@str} BRAINS"
  end

end

tests目录的内容如下:

4096 Mar  9 14:28 ..
 106 Mar  9 14:36 zombifier.rb
4096 Mar  9 14:38 .idea
4096 Mar  9 14:53 coverage
 323 Mar  9 15:00 Zombifier_test.rb_bk
 379 Mar  9 15:19 Zombifier_test.rb
4096 Mar  9 15:19 .

1 个答案:

答案 0 :(得分:1)

在测试文件之上需要类似的东西(在需要之前):

$: << "../src" # path to zombifier.rb relative to zombifier_test.rb