ruby – 如何使用gets.chomp测试函数?
发布时间:2020-12-17 04:36:26 所属栏目:百科 来源:网络整理
导读:我有一个使用gets.chomp的简单函数,如下所示: def welcome_user puts "Welcome! What would you like to do?" action = gets.chompend 我想使用ruby内置的TestCase套件测试它,如下所示: class ViewTest Test::Unit::TestCase def test_welcome welcome_use
我有一个使用gets.chomp的简单函数,如下所示:
def welcome_user puts "Welcome! What would you like to do?" action = gets.chomp end 我想使用ruby内置的TestCase套件测试它,如下所示: class ViewTest < Test::Unit::TestCase def test_welcome welcome_user end end 问题是,当我运行该测试时,gets.chomp会停止测试,因为它需要用户输入某些内容.有没有办法可以使用ruby模拟用户输入? 解决方法
您首先将该方法的两个问题分开:
def get_action gets.chomp end def welcome_user puts "Welcome to Jamaica and have a nice day!" action = get_action return "Required action was #{action}." end 然后你分别测试第二个. require 'minitest/spec' require 'minitest/autorun' describe "Welcoming users" do before do def get_action; "test string" end end it "should work" do welcome_user.must_equal "Required action was test string." end end 至于第一个,你可以 >手动测试并依赖它不会破坏(建议的方法,TDD不是宗教). 虽然这是对你的问题的实际答案,但我不知道如何做2.我只知道如何模仿浏览器后面的用户(watir-webdriver)而不是shell会话后面. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |