lundi 3 août 2015

How to test a command line class with RSpec?

I have a command line class along the lines of:

class CLI
  def initialize(*params)
    @params = params
    user_input = gets.chomp.strip.downcase
    process_input(user_input)
  end

  private

  def process_input(user_input)
    if user_input == "some_string"
      puts @params[0].to_s
      exit
    else
      puts "Hello world"
      exit
    end
  end
end

CLI#new uses gets.chomp. If the user enters a certain string, then it prints out a new string and then exits. I want to test that the correct string is printed out if the user enters the correct input. How can I do this with RSpec? I have never written tests for a command line interface.

Also, should I perhaps use a class method instead of initialize?

Aucun commentaire:

Enregistrer un commentaire