繁体   English   中英

Ruby单元测试(gets.chomp)

[英]Ruby unit test (gets.chomp)

我有红宝石课:

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

并测试:

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

我只想测试correct_length? 方法。 运行测试时,我必须在测试开始之前输入一些字符。 我应该如何重写此示例以仅测试一种方法(不运行gets.chomp

if __FILE__ == $0 ... end包围调用main的部分; 如果运行测试,并且以脚本作为入口点执行该部分,则该部分将不会执行。

if __FILE__ == $0
  instance = Sample.new
  instance.main
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM