Techioz Blog

Rspecを使用してmethod_optionsをスタブするにはどうすればよいですか?

概要

Class CLI < Thor
  desc "run [age]" "user passes age"
  method_option :age,type:'numeric'
  def run
    variable=options[:a] if options[:a].present?
    call_method(a)
  end
end

次の行をテストしたい場合、Rspec を使用して options[:a] をモックするにはどうすればよいですか?

variable=options[:a] if options[:a].present?

また、ローカル変数が更新されたかどうかをテストするにはどうすればよいですか?

解決策

「オプション」をモックすることができます

  let(:cli) { CLI.new }
  allow(cli).to receive(:options){ { a:2 } }