You can stub Kernel#gets
, except that it is mixed into the object, so stub it there:
class Mirror
def echo
print "enter something: "
response = gets.chomp
puts "#{response}"
end
end
require 'rspec'
describe Mirror do
it "should echo" do
@mirror = Mirror.new
@mirror.stub!(:gets) { "phrase
" }
@mirror.should_receive(:puts).with("phrase")
@mirror.echo
end
end
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…