【问题标题】:How to remove desc messages from rspec when using thor?使用 thor 时如何从 rspec 中删除 desc 消息?
【发布时间】:2015-09-02 09:01:56
【问题描述】:

现在我在 Rails 项目中使用thor

我写了这些代码:

lib/tasks/my_task.rb

require 'thor'

module Tasks
  class MyTask < Thor
    desc 'My Batch', 'This is my awesome batch'
    option :date
    def execute(type)
      # do_something
    end
  end
end

Tasks::MyTask.start(ARGV)

spec/lib/tasks/my_task_spec.rb

require 'spec_helper'

describe 'Test my task' do
  context 'With date option' do
    before do
      @option = { date: '20150903' }
    end

    it 'Can insert to db' do
      expect do
        Tasks::MyTask.new.invoke(:execute, ['commit'], @option)
      end.to change(ProductTable, :count).by(1)
    end
  end
end

问题是当我运行bundle exec rspec时,它显示:

Run options: exclude {:heavy=>true}
..................................................................................................................................................................................................****************************...................................................
........................................................************.......................................................................................................................................................................................................******
Commands:
  rspec help [COMMAND]  # Describe available commands or one specific command
  rspec My Batch          # This is my awesome batch
.................................................................................................................................................................................................................................................................................

为什么在这里显示 desc 消息?如何配置删除它们?

【问题讨论】:

  • 这就是Thor 的工作方式,目前尚不清楚您为什么要启动Thor CLI 并期望它不能按其工作方式工作
  • 如果您不想显示描述,请不要定义描述。这不是强制性的。
  • @WandMaker 因为在我的项目中,我应该手动运行批处理并使用一些选项。
  • @mudasobwa 如果我不使用它。我的测试不会通过。

标签: ruby-on-rails ruby testing rspec thor


【解决方案1】:

尚未对其进行测试,但我想您可能可以使用以下方法对其进行修补:

module Thor
  def puts(*args, &block)
    # do nothing
  end
end

编辑:实际上,这里的处理方式可能略有不同:https://github.com/erikhuda/thor/blob/master/lib/thor/shell/basic.rb

看起来你可以去掉Thor::Shell::Basic#stdout#stderr

【讨论】:

  • 谢谢。你的意思是我应该重写 Thor 模块?
  • 在你的库中修改一些方法来进行测试并不是不寻常的。它为您的代码添加了依赖项,但我发现能够测试功能通常是值得的
猜你喜欢
  • 2016-09-21
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2015-07-08
  • 2014-06-07
  • 1970-01-01
相关资源
最近更新 更多