Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test rspec user input and output with Highline?

I'd like to test response to user input. That input is queried using Highline:

def get_name
  return HighLine.new.ask("What is your name?")
end

I'd like to do something similar to this question and put it in my test:

STDOUT.should_receive(:puts).with("What is your name?")
STDIN.should_receive(:read).and_return("Inigo Montoya")
MyClass.new.get_name.should == "Inigo Montoya"

What is the correct way to do this with Highline?

like image 304
Ovesh Avatar asked Jan 30 '26 18:01

Ovesh


2 Answers

The best way to find out how to test Highline is to see how the author tests his package.

class TestHighLine < Test::Unit::TestCase
  def setup
    @input    = StringIO.new
    @output   = StringIO.new
    @terminal = HighLine.new(@input, @output)..
  end
..
  def test_agree
    @input << "y\nyes\nYES\nHell no!\nNo\n"
    @input.rewind

    assert_equal(true, @terminal.agree("Yes or no?  "))
    assert_equal(true, @terminal.agree("Yes or no?  "))
    assert_equal(true, @terminal.agree("Yes or no?  "))
    assert_equal(false, @terminal.agree("Yes or no?  "))
....
    @input.truncate(@input.rewind)
    @input << "yellow"
    @input.rewind

    assert_equal(true, @terminal.agree("Yes or no?  ", :getc))
  end


   def test_ask
     name = "James Edward Gray II"
     @input << name << "\n"
     @input.rewind

     assert_equal(name, @terminal.ask("What is your name?  "))
 ....
     assert_raise(EOFError) { @terminal.ask("Any input left?  ") }
   end

Etc., as shown in his code. You can find this information in the highline source paying close attention to the setup, which I have highlighted in the link.

Notice how he uses the STDIN IO pipe to act in the place of typing the keys on the keyboard.

What this indicates, really, is that you don't need to use highline to test that kind of thing. The setup in his tests are really key here. Along with his use of StringIO as an object.

like image 169
vgoff Avatar answered Feb 02 '26 11:02

vgoff


Highline already has it's own tests to make sure that it outputs to STDOUT and reads from STDIN. There is no reason to write these types of tests. It's the same reason that you would not write ActiveRecord tests that make sure attributes can be saved and read from the database.

However... it would be extremely useful if there were a framework for Highline that works in a similar fashion as Capybara for web forms... something that actually drives the input from the UI and tests the logic of your command-line utility.

For example, the following kind of hypothetical test would be nice:

run 'my_utility.rb'
highline.should :show_menu
select :add
highline.should(:ask).with_prompt("name?")
enter "John Doe"
output.should == "created new user"
like image 23
user132447 Avatar answered Feb 02 '26 11:02

user132447



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!