Craig Rinde
Craig Rinde

Reputation: 95

Simple Ruby Test Not Working

I am going through Learn Ruby the Hard Way and its been great up until now.I can't for the life of me figure out why my test is not working. I have spent a few hours trying to figure it out and I am sure it is something simple.

To me it looks like it is not finding the scan method but I can seem to figure out why. Any help is appreciated!

Here is the code

test_lexicon.rb file

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 file

class Lexicon

  def scan(text)
    return text
  end

end

Here is the error from the terminal:

Running tests:

[1/1] TestNAME#test_directions = 0.00 s 1) Error: test_directions(TestNAME): NoMethodError: undefined method scan' for Lexicon:Class /Users/guest1/Dropbox/ruby_projects/play/ex48/tests/test_lexicon.rb:7:intest_directions' /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:inrun' /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:inmap' /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:inblock in _run_suites' /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' /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:inrun_tests' /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:ineach' /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:inrun' /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:inrun' /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:inrun_once' /Users/guest1/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/test/unit.rb:365:in `block in autorun'

Finished tests in 0.004470s, 223.7136 tests/s, 0.0000 assertions/s. 1 tests, 0 assertions, 0 failures, 1 errors, 0 skips

ruby -v: ruby 2.0.0p481 (2014-05-08 revision 45883) [x86_64-darwin13.1.0] rake aborted!

Upvotes: 0

Views: 237

Answers (1)

jcm
jcm

Reputation: 1789

You've defined an instance method in your class:

def scan(text)
  return text
end

but are trying to call a class method in your test:

Lexicon.scan("north")

To make it a class method do:

def self.scan(text)
  return text
end

Edit: this answer does not address whether you should use a class, instance, or module method.

Upvotes: 3

Related Questions