문제

I have ruby class:

class Sample
  def main
    zipcode = gets.chomp

    if correct_length?(zipcode)
    end

  end

  def correct_length?(string)
    true if string.size == 5
  end
end

instance = Sample.new
instance.main

and test for it:

require_relative 'sample'
require 'test/unit'

class TestSample < Test::Unit::TestCase
  def test_correct_length?
    zipcode = '10234'
    assert_equal(true, Sample.new.correct_length?(zipcode))
  end
end

I want to test just correct_length? method. When I run the test I must enter some characters before tests start. How should I rewrite this example to test just a method (not running gets.chomp)

도움이 되었습니까?

해결책

Surround the part that call main with if __FILE__ == $0 ... end; If you run test, that part will not executed while the part is executed if the script is executed as an entry point.

if __FILE__ == $0
  instance = Sample.new
  instance.main
end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top