【问题标题】:How do I test a function with gets.chomp in it?如何测试带有gets.chomp 的函数?
【发布时间】:2013-06-01 16:01:37
【问题描述】:

我有一个使用 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 模拟用户输入?

【问题讨论】:

    标签: ruby unit-testing shell testing


    【解决方案1】:

    您可以创建一个pipe 并将其“读取结束”分配给$stdin。写入管道的“写入端”然后模拟用户输入。

    这里有一个小辅助方法with_stdin 用于设置管道的示例:

    require 'test/unit'
    
    class View
      def read_user_input
        gets.chomp
      end
    end
    
    class ViewTest < Test::Unit::TestCase
      def test_read_user_input
        with_stdin do |user|
          user.puts "user input"
          assert_equal(View.new.read_user_input, "user input")
        end
      end
    
      def with_stdin
        stdin = $stdin             # remember $stdin
        $stdin, write = IO.pipe    # create pipe assigning its "read end" to $stdin
        yield write                # pass pipe's "write end" to block
      ensure
        write.close                # close pipe
        $stdin = stdin             # restore $stdin
      end
    end
    

    【讨论】:

    • 感谢IO.pipe,你在那里教会了我一些东西。
    • 谢谢,喜欢这个方法!
    【解决方案2】:

    您首先将方法的两个关注点分开:

    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
    

    至于第一个,你可以

    1. 手动测试它并相信它不会损坏(推荐的方法,TDD 不是一种宗教)。
    2. 获取有问题的shell的颠覆版本,并使其模仿用户,并进行比较 get_action 是否确实得到了用户输入的内容。

    虽然这是对您问题的实际答案,但我不知道该怎么做 2.,我只知道如何在浏览器 (watir-webdriver) 后面模仿用户,而不是在 shell 会话后面。

    【讨论】:

      【解决方案3】:

      您可以注入 IO 依赖项。 gets 读取自 STDIN,即类 IO。如果您将另一个IO 对象注入到您的类中,您可以在测试中使用StringIO。像这样的:

      class Whatever
        attr_reader :action
      
        def initialize(input_stream, output_stream)
          @input_stream = input_stream
          @output_stream = output_stream
        end
      
        def welcome_user
          @output_stream.puts "Welcome! What would you like to do?"
          @action = get_input
        end
      
      private
      
        def get_input
          @input_stream.gets.chomp
        end
      
      end
      

      测试:

      require 'test/unit'
      require 'stringio'
      require 'whatever'
      
      class WhateverTest < Test::Unit::TestCase
        def test_welcome_user
          input = StringIO.new("something\n")
          output = StringIO.new
          whatever = Whatever.new(input, output)
      
          whatever.welcome_user
      
          assert_equal "Welcome! What would you like to do?\n", output.string
          assert_equal "something", whatever.action
        end
      end
      

      这允许您的类与任何 IO 流(TTY、文件、网络等)进行交互。

      要在生产代码中的控制台上使用它,请传入STDINSTDOUT

      require 'whatever'
      
      whatever = Whatever.new STDIN, STDOUT
      whatever.welcome_user
      
      puts "Your action was #{whatever.action}"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-04
        • 1970-01-01
        • 1970-01-01
        • 2016-06-21
        • 1970-01-01
        • 2018-09-20
        • 2019-02-13
        • 2021-07-28
        相关资源
        最近更新 更多