rspec 命令行变量输入 [英] rspec commandline variable input
本文介绍了rspec 命令行变量输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 ruby 脚本,我正在尝试使用 rspec 进行测试.有没有办法将变量传递给命令行(即通过 rspec 输入键盘数据以获取")
I have a ruby script I'm trying to test with rspec. Is there a way to pass variables to the commandline (ie enter keyboard data via rspec to "gets")
示例:
username = gets.chomp
推荐答案
你可以存根 Kernel#gets
,除了它混入对象中,所以存根在那里:
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\n" }
@mirror.should_receive(:puts).with("phrase")
@mirror.echo
end
end
这篇关于rspec 命令行变量输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文