【问题标题】:Testing interactive thor tasks测试交互式雷神任务
【发布时间】:2012-06-07 16:30:58
【问题描述】:

我有以下雷神命令:

require 'highline'
class Import < Thor

  desc "files", "Import files into the database"
  method_option "path", :required => true, :desc => "Path to folder containing new  files", :aliases => "-p", :type => :string

  def files
    require './config/environment'

    line = HighLine.new
    line.say(line.color("Identified files as Version 15 (English)", :green))
    if line.agree(line.color("Are you sure you want to import?", :yellow))
      line.say(line.color("Finished.  Imported 70,114 items", :green))
    else
      line.say(line.color("Aborting...", :red))
    end
  end

end

现在,显然,目前这只是在屏幕上输出一些语言。但是,我需要做的是为测试输出的命令编写一个测试,正如我所期望的那样,当我开始从事繁重的工作时,我可以将这些东西存根。

我看过Aruba,但出于某种原因,这似乎不喜欢交互性,目前尚不清楚原因。

因此,是否有人对如何测试(使用 RSpec)有任何想法?

【问题讨论】:

    标签: ruby-on-rails ruby cucumber thor


    【解决方案1】:

    Aruba 是一套非常完整的用于测试命令行应用程序的步骤。如果它对您不起作用,可能是因为 aruba 将所有文件操作默认为 tmp/aruba

    但是 neimOo 关于如何使用 aruba 编写场景是正确的

    When I run `thor import` interactively
    And I type "yes"
    

    【讨论】:

      【解决方案2】:

      以下是使用 Aruba 执行此操作的方法

      Scenario: Test import
        When I run `thor import` interactively
        And I type "yes"
        Then the stdout should contain "Finished.  Imported 70,114 items"
      

      在这里您可以找到很多 aruba 示例 https://github.com/cucumber/aruba/blob/master/features/interactive.feature

      这是实现本身 https://github.com/cucumber/aruba/blob/master/lib/aruba/cucumber.rb

      【讨论】:

      • 我认为你也可以要求 'aruba/api' 并使用在那里实现的方法 github.com/cucumber/aruba/blob/master/lib/aruba/api.rb 即使在 RSpec 中
      • 这不会因为 tmp/aruba 中没有任务导入而失败吗?
      • 尼尔·米德尔顿写的雷神脚本不会失败,因为它只会将smth写入输出。所以如果我输入yes,它将返回我所期望的。但是当我们有一些文件操作代码时,我们可以使用相对路径,一切都应该工作
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多