0

I have a ruby script that takes the prices of the books from the user at run time. I'm new to writing RSpec tests for Ruby scripts. I'm using Ruby 1.9.3-p327 and rspec 2.11.0. You can clone my project from Github this link.

My Rspec Test is intended to test if the newly instantiated object belongs to a specific class.

My test passes successfully if I comment line number 32 in the ruby script .

Somehow when I uncomment that line, I'm getting the error below that is to do with user input. I'm not even testing this as part of my spec file, but still I'm landing up with this error. I don't know exactly why and how could I work my way around this issue.

The Last Samurai: /home/mohnish/xxx/yyy/sample_pocs/book/book.rb:11:in `gets': Is a directory - spec (Errno::EISDIR)
    from /home/mohnish/xxx/yyy/sample_pocs/book/book.rb:11:in `gets'
    from /home/mohnish/xxx/yyy/sample_pocs/book/book.rb:11:in `block in get_prices'
from /home/mohnish/xxx/yyy/sample_pocs/book/book.rb:9:in `each'
    from /home/mohnish/xxx/yyy/sample_pocs/book/book.rb:9:in `inject'
    from /home/mohnish/xxx/yyy/sample_pocs/book/book.rb:9:in `get_prices'
from /home/mohnish/xxx/yyy/sample_pocs/book/book.rb:49:in `<top (required)>'
    from /home/mohnish/xxx/yyy/sample_pocs/book/spec/spec_helper.rb:1:in `require_relative'
    from /home/mohnish/xxx/yyy/sample_pocs/book/spec/spec_helper.rb:1:in `<top (required)>'
from /home/mohnish/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /home/mohnish/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /home/mohnish/xxx/yyy/sample_pocs/book/spec/book_spec.rb:1:in `<top (required)>'
from /home/mohnish/.rvm/gems/ruby-1.9.3-p327@book_set/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
    from /home/mohnish/.rvm/gems/ruby-1.9.3-p327@book_set/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
    from /home/mohnish/.rvm/gems/ruby-1.9.3-p327@book_set/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
from /home/mohnish/.rvm/gems/ruby-1.9.3-p327@book_set/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
    from /home/mohnish/.rvm/gems/ruby-1.9.3-p327@book_set/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
    from /home/mohnish/.rvm/gems/ruby-1.9.3-p327@book_set/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
from /home/mohnish/.rvm/gems/ruby-1.9.3-p327@book_set/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'

I also want to test for the user input for this example. It would be great if you could throw some light on how could I got about the same as well. I found some places where I can get started with testing user input like eg 1 and eg 2, but I'm mainly looking how to test user input for a number of elements that belong to a hash

Thanks.

Community
  • 1
  • 1
boddhisattva
  • 6,908
  • 11
  • 48
  • 72

1 Answers1

0

Use

STDIN.gets.chomp

See this answer.

The best way to test this is to mock IO#gets with an object and method that returns a canned response. An RSpec mock would look something like:

STDIN.should_receive(:gets).and_return("A Book Title")

See this answer for a couple more examples. Set these up in the appropriate before :each blocks and they will intercept the call to gets in your test.

Community
  • 1
  • 1
Sean Redmond
  • 3,974
  • 22
  • 28
  • Thanks Sean. That fixed the error. I probably should have refined my search to "`gets': Is a directory - spec". I'm Learning the way.. :). Any inputs on how could I write a spec for user input of Hash elements ? – boddhisattva Jul 14 '13 at 14:46
  • The user is inputing some string that gets converted to a Hash, or the method is expecting an actual Hash? – Sean Redmond Jul 14 '13 at 14:50
  • The user input is a string and each such string is stored as a value for each key element in the Hash. – boddhisattva Jul 14 '13 at 16:46
  • I'm accepting your answer as it mainly did solve the gets error for me. Thank you. – boddhisattva Jul 21 '13 at 12:06