Reputation: 3228
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)
Upvotes: 1
Views: 268
Reputation: 368954
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
Upvotes: 2