【问题标题】:Testing ARGV options with rspec. How to expect a method to call a method from another module使用 rspec 测试 ARGV 选项。如何期望一个方法从另一个模块调用一个方法
【发布时间】:2020-11-22 18:36:10
【问题描述】:

我正在用 Ruby 构建一个 CLI,并且我正在使用 ARGV 在命令行中传递选项和参数。我有一个在执行 CLI 时触发的调用方法。 调用方法如下:

module Eltiempo
  class CLI
    def call
      help_menu if ARGV.count.zero?
      case ARGV[0]
      when '-today'
        raise NoCityError if ARGV[1].nil?

        Eltiempo.today(ARGV[1])
      when '-av_min'
        raise NoCityError if ARGV[1].nil?

        Eltiempo.av_min(ARGV[1])
      when '-av_max'
        raise NoCityError if ARGV[1].nil?

        Eltiempo.av_max(ARGV[1])
      when '-h'
        help_menu
      end
    end

在使用 rspec 对第一个案例选项(-today)进行测试时,我编写了以下代码:

RSpec.describe Eltiempo do
  describe '#call' do
    context 'given -today' do
      let(:ARGV) { ['-today', 'Barcelona'] }
      it 'calls function to return today\'s weather' do
        expect(Eltiempo::CLI.new.call).to receive(Eltiempo.today).with(ARGV[1])
      end
    end
  end
end

但是,当运行 rspec 时,它没有通过测试,它说:

Failure/Error:
       def self.today(city_name)
         max = max_today(city_name)
         min = min_today(city_name)
         puts "Weather today in #{city_name.capitalize}:
           - Maximum: #{max}°C
           - Minimum: #{min}°C"
       end
     
     ArgumentError:
       wrong number of arguments (given 0, expected 1)

它试图在 Eltiempo 模块中调用方法 self.today(city_name) 并在没有 city_name 参数的情况下运行,但是,我不希望它运行我只想检查选项和参数 -today Barcelona 时的方法运行时,它调用Eltiempo.today(ARGV[1])

为什么运行self.today(city_name)

【问题讨论】:

    标签: ruby rspec


    【解决方案1】:

    方法调用来自屋内!

    expect(Eltiempo::CLI.new.call).to receive(Eltiempo.today).with(ARGV[1])
                                              ^^^^^^^^^^^^^^
    

    这就是它的样子,不带参数地调用Eltiempo.today

    设置期望表示您期望特定对象接收特定方法。在这种情况下,这个对象是类Eltiempo。该方法通过名称作为符号传递。在设置了today 将在Eltiempo 上被调用的预期之后,然后运行预期调用Eltiempo.today 的代码

    expect(Eltiempo).to receive(:today).with(ARGV[1])
    Eltiempo::CLI.new.call
    

    RSpec 已将 Eltiempo 上的方法 today 替换为记录它是否被调用并且不返回任何内容的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 2015-06-16
      • 1970-01-01
      • 2020-08-29
      相关资源
      最近更新 更多