【问题标题】:How to test a script that generates files如何测试生成文件的脚本
【发布时间】:2011-02-19 18:54:31
【问题描述】:

我正在创建一个 Rubygem,它可以让我生成 jekyll 帖子文件。我开发这个项目的原因之一是学习 TDD。这个 gem 在命令行上是严格可用的,它必须进行一系列检查以确保它找到 _posts 目录。这取决于两件事:

  1. location 选项是否已通过
    • 该位置选项有效吗?
  2. 位置选项未通过
    • posts 目录是否在当前目录中?
    • posts 目录是当前工作目录吗?

那时,我真的很难测试应用程序的这一部分。所以我有两个问题:

  • 是否可以跳过对上述应用程序的小部分进行测试?
  • 如果没有,您如何使用 minitest 在 ruby​​ 中测试文件操作?

【问题讨论】:

    标签: ruby tdd directory command-line-interface minitest


    【解决方案1】:

    我见过的一些项目将它们的命令行工具实现为 Command 对象(例如:Rubygemsmy linebreak gem)。这些对象使用 ARGV 进行初始化,只需调用或执行方法即可启动整个过程。这使这些项目能够将其命令行应用程序放入虚拟环境中。例如,它们可以将输入和输出流对象保存在命令对象的实例变量中,以使应用程序独立于使用 STDOUT/STDIN。因此,可以测试命令行应用程序的输入/输出。就像我想象的那样,您可以将当前工作目录保存在一个实例变量中,以使您的命令行应用程序独立于您的实际工作目录。然后,您可以为每个测试创建一个临时目录,并将其设置为您的 Command 对象的工作目录。

    现在是一些代码:

    require 'pathname'
    
    class MyCommand
      attr_accessor :input, :output, :error, :working_dir
    
      def initialize(options = {})
        @input = options[:input] ? options[:input] : STDIN
        @output = options[:output] ? options[:output] : STDOUT
        @error = options[:error] ? options[:error] : STDERR
        @working_dir = options[:working_dir] ? Pathname.new(options[:working_dir]) : Pathname.pwd
      end
    
      # Override the puts method to use the specified output stream
      def puts(output = nil)
        @output.puts(output)
      end
    
      def execute(arguments = ARGV)
        # Change to the given working directory
        Dir.chdir(working_dir) do
          # Analyze the arguments
          if arguments[0] == '--readfile'
            posts_dir = Pathname.new('posts')
            my_file = posts_dir + 'myfile'
            puts my_file.read
          end
        end
      end
    end
    
    # Start the command without mockups if the ruby script is called directly
    if __FILE__ == $PROGRAM_NAME
      MyCommand.new.execute
    end
    

    现在在您的测试设置和拆卸方法中,您可以执行以下操作:

    require 'pathname'
    require 'tmpdir'
    require 'stringio'
    
    def setup
      @working_dir = Pathname.new(Dir.mktmpdir('mycommand'))
      @output = StringIO.new
      @error = StringIO.new
    
      @command = MyCommand.new(:working_dir => @working_dir, :output => @output, :error => @error)
    end
    
    def test_some_stuff
      @command.execute(['--readfile'])
    
      # ...
    end
    
    def teardown
      @working_dir.rmtree
    end
    

    (在示例中,我使用了 Pathname,这是一个非常好的面向对象的文件系统 API,来自 Ruby 的标准库和 StringIO,它对于模拟 STDOUT 非常有用,因为它是一个流入简单字符串的 IO 对象)

    在实际测试中,您现在可以使用 @working_dir 变量来测试文件的存在或内容:

    path = @working_dir + 'posts' + 'myfile'
    path.exist?
    path.file?
    path.directory?
    path.read == "abc\n"
    

    【讨论】:

      【解决方案2】:

      根据我的经验(因此这是非常主观的),我认为有时在某些难以测试的领域跳过单元测试是可以的。您需要找出您得到的回报以及测试的成本。我的经验法则是不测试课程的决定应该是非常不寻常的(大约不到 300 个课程中的 1 个)

      如果您要测试的内容非常困难,由于与文件系统的依赖关系,我认为您可以尝试提取与文件系统交互的所有位。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-10
        • 1970-01-01
        相关资源
        最近更新 更多