简单的Ruby测试不起作用

时间:2014-07-30 02:24:16

标签: ruby

我正在经历“学习Ruby”的艰辛之路,直到现在还很棒。我不能为我的生活找出为什么我的测试不起作用。我花了几个小时试图解决它,我确信这很简单。

对我而言,它似乎没有找到扫描方法,但我似乎可以找出原因。任何帮助表示赞赏!

这是代码

test_lexicon.rb文件

require_relative '../lib/ex48/lexicon.rb'
require "test/unit"

class TestNAME < Test::Unit::TestCase

  def test_directions
    assert_equal(Lexicon.scan("north"), 'north')
  end

end

lexicon.rb文件

class Lexicon

  def scan(text)
    return text
  end

end

以下是来自终端的错误:

运行测试:

[1/1] TestNAME#test_directions = 0.00 s   1)错误: test_directions(测试名): NoMethodError:未定义的方法scan' for Lexicon:Class /Users/guest1/Dropbox/ruby_projects/play/ex48/tests/test_lexicon.rb:7:in test_directions&#39;     /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/minitest/unit.rb:1301:in run' /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/test/unit/testcase.rb:17:in运行&#39;     /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/minitest/unit.rb:919:in block in _run_suite' /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/minitest/unit.rb:912:in map&#39;     /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/minitest/unit.rb:912:in _run_suite' /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/test/unit.rb:657:in阻止_run_suites&#39;     /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/test/unit.rb:655:in each' /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/test/unit.rb:655:in _ run_suites&#39;     /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/minitest/unit.rb:867:in _run_anything' /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/minitest/unit.rb:1060:in run_tests&#39;     /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/minitest/unit.rb:1047:in block in _run' /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/minitest/unit.rb:1046:in每个&#39;     /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/minitest/unit.rb:1046:in _run' /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/minitest/unit.rb:1035:in运行&#39;     /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/test/unit.rb:21:in run' /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/test/unit.rb:774:in run&#39;     /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/test/unit.rb:366:in block (2 levels) in autorun' /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/test/unit.rb:27:in run_once&#39;     /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/test/unit.rb:365:in,clock in autorun&#39;

完成测试0.004470s,223.7136次测试/秒,0.0000断言/秒。 1个测试,0个断言,0个失败,1个错误,0个跳过

ruby​​ -v:ruby 2.0.0p481(2014-05-08修订版45883)[x86_64-darwin13.1.0] 耙子流产了!

1 个答案:

答案 0 :(得分:3)

您已在班级中定义了一个实例方法:

def scan(text)
  return text
end

但是正在尝试在测试中调用类方法:

Lexicon.scan("north")

要使它成为类方法,请执行:

def self.scan(text)
  return text
end

编辑:这个答案没有解决你是否应该使用类,实例或模块方法。