Q:

No implicit conversion of nil into String when stubbing 'gets' method

I'm having problems with testing this simple command line game.  My test:
it 'should ask the user name' do
  allow(@game).to receive(:get_name) { 'Patrick'}
  expect{@game.show_name}.to output("Your name is Patrick.\n").to_stdout
end

My code:
def get_name
   @name = gets.chomp
end

  def show_name
    puts 'Your name is ' + @name + '.'
  end

Error message:
1) Game setup should ask the user name
     Failure/Error: puts 'Your name is ' + @name + '.'
 TypeError:
   no implicit conversion of nil into String
 # ./lib/game.rb:24:in `+'
 # ./lib/game.rb:24:in `show_name'
 # ./spec/game_spec.rb:12:in `block (4 levels) in <top (required)>'
 # ./spec/game_spec.rb:12:in `block (3 levels) in <top (required)>'

A:

You don't need to stub get_name you just need to control the input to gets.chomp
Change your code to...
def get_name
   @name = $stdin.gets.chomp
end

Then your test should be...
require 'stringio'

describe 'test game' do

  before do
    $stdin = StringIO.new("Patrick\n")
  end

  after do
    $stdin = STDIN
  end

  it 'should ask the user name' do
    @game.get_name
    expect{@game.show_name}.to output("Your name is Patrick.\n").to_stdout
  end

end

