You can stub out standard input stream like this:
require 'stringio'
def capture_name
$stdin.gets.chomp
end
describe 'capture_name' do
before do
$stdin = StringIO.new("James T. Kirk\n")
end
after do
$stdin = STDIN
end
it "should be 'James T. Kirk'" do
expect(capture_name).to be == 'James T. Kirk'
end
end