I am building a CLI in Ruby and I am using ARGV to pass options and arguments in the command line. I have a call method that is triggered when the CLI is executed. The call method is as follows:
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
When doing the test with rspec for the first case option (-today) I have written the following code:
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
However, when running rspec it is not passing the test and it says:
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)
It is trying to call the method self.today(city_name) in Eltiempo module and to run in without the city_name argument, however, I do not want it to run the method I just want to check that when the option and argument -today Barcelona is run, it calls Eltiempo.today(ARGV[1]).
Why is it running self.today(city_name)?