需要用户输入的方法的Ruby minitest测试输出

需要用户输入的方法的Ruby minitest测试输出,ruby,stdin,minitest,assertions,Ruby,Stdin,Minitest,Assertions,决心 问题1:我想在下面的课程中为file_choice_reader编写一个测试 该类将特定类型的文件列表打印到命令行,并允许用户通过键入索引号来选择一个 class File_chooser #shortened for readability def file_choice_suggester file_list = file_list_generator if file_list.count > 0 file_list.each_with_i

决心

问题1:我想在下面的课程中为file_choice_reader编写一个测试

该类将特定类型的文件列表打印到命令行,并允许用户通过键入索引号来选择一个

class File_chooser

  #shortened for readability

  def file_choice_suggester
    file_list = file_list_generator
    if file_list.count > 0
      file_list.each_with_index do |file, index|
        puts index.to_s + ' ' + file
      end
    else 
      puts 'Neither .fcv nor .tmpl nor .ipa nor .apf files in directory.'  
    end
    file_list
  end

  def file_choice_reader
    unless File.exists? 'Cookie.txt'
      file_list = file_choice_suggester
      puts 'Choose file by typing index number!'
      chosen_file = STDIN.gets.chomp
      if /[^0-9]/.match(chosen_file) || chosen_file.to_i >= file_list.count
        abort ('No valid index number.')
      else 
        chosen_file = chosen_file.to_i
      end
      cookie_writer(  file_list[chosen_file].to_s )
      system 'cls'
      puts 'You chose file: ' + file_list[chosen_file].to_s
      path_and_file = file_list[chosen_file].to_s
    else
      self.hints_hash = hints_hash.merge( 'cookie_del' => '* Change file by typing command: del Cookie.txt *' )
      pre_chosen_file = File.read('Cookie.txt')
      path_and_file = pre_chosen_file.chomp.to_s
    end
    path_and_file
  end

end
我的测试看起来像这样,提示我键入索引号,但它仍然显示输出为:

class TestFile_chooser < MiniTest::Unit::TestCase 
  def setup
    @file_chooser = File_chooser.new
  end

  def test_file_choice_reader_produces_confirmation_output
    assert_output( /You chose file/ ) { @file_chooser.file_choice_reader }
  end 
end
这个测试通过了。但它留给我的是1次运行,2次断言。这让我感到困惑。一次运行一次测试如何产生2??断言

我很高兴能得到帮助。互联网上的小型讨论似乎没有涵盖这些内容。也许这太基本了

我也很感谢评论中关于该准则的所有其他评论。谢谢你的帮助

更新问题1

在下面回复的帮助下,我的最新版本的测试使用了中的示例

测试运行时没有出现错误。。。但是它仍然告诉我:反驳失败。没有消息

谢谢你迄今为止的帮助!也谢谢你给我的提示,告诉我如何解决这个问题

[在此处添加代码很难在注释中阅读。]

更新2问题1

我按照另一个问题的建议明确要求使用minitest gem。我把它放在我的测试文件代码上面:

require 'rubygems'
gem 'minitest'
require 'minitest/autorun'
require_relative 'falcon'
如果这是多余的,请让我知道

以下测试代码现在不再产生错误或失败:

  def test_file_choice_suggester_produces_output
    assert_output( /apf|fcv|tmpl|ipa/ ) {  @file_chooser.file_choice_suggester }
  end 
谢谢大家的帮助

对于1:

一种方法可以是通过重写puts方法来删除测试正在使用的IO对象。另一种方法是重构您的设计,使您的方法更小,更易于测试

puts覆盖将如下所示:

在您的测试中:

@file_chooser = File_chooser.new
printed = nil
@file_chooser.instance_eval do
  # Open up the instance and stub out the puts method to save to a local variable
  define_method :puts, Proc.new {|arg| printed = arg}
end
# Run code
refute printed.nil?

或者,您可以在$STDOUT对象上运行一个期望值,以确保它得到正确的调用参见Mocha for Ruby

2它可能与生成多个断言的assert\u输出方法有关。就风格而言,我推荐GitHub的风格指南:1不确定问题到底是什么,但应该可以删除用户输入或将输入流作为参数或其他东西传递给你!问题是:如何编写一个测试来确保我的用户获得确认消息。输出不是方法的直接输出,而是仅在用户输入sdtin后显示。测试当前告诉我该方法输出一个空字符串。用户输入后,情况并非如此。我需要如何编写测试?我希望这更清楚。谢谢!不幸的是,这对我不起作用。错误消息是:falcon_test.rb:90:语法错误,意外的“{”,期望关键字_end define_method,:put{arg|printed=arg}^falcon_test.rb:90:语法错误,意外的“}”,期望关键字_end falcon_test.rb:116:语法错误,意外的$end,期望关键字_endI将代码放入def test。。。。终止不知道我做错了什么。谢谢回复。对不起,我把语法弄错了。现在就试试,谢谢!在阅读了define_方法是如何私有的并重复了此页面上的hack之后,测试运行时没有出现错误:@file_chooser.instance_eval do self.create_方法:puts{arg | printed=arg}end。。。但是它仍然告诉我我的输出是:In stdout。预期/apf | fcv | tmpl | ipa/匹配。谢谢你。我确实学到了一些东西。感谢所有提示如何解决它!抱歉,错误消息混淆了。信息是:反驳失败。没有消息。
  def test_file_choice_suggester_produces_output
    assert_output( /apf|fcv|tmpl|ipa/ ) {  @file_chooser.file_choice_suggester }
  end 
@file_chooser = File_chooser.new
printed = nil
@file_chooser.instance_eval do
  # Open up the instance and stub out the puts method to save to a local variable
  define_method :puts, Proc.new {|arg| printed = arg}
end
# Run code
refute printed.nil?