Ruby Rspec gets剥离测试将不运行
我正在尝试指定当前方法的规格:Ruby Rspec gets剥离测试将不运行,ruby,rspec,Ruby,Rspec,我正在尝试指定当前方法的规格: class CLIinterface def units_inputs puts "\n\n________________________________________________________________________________________________________________________\nEnter the desired unit class of measurement desired for cu
class CLIinterface
def units_inputs
puts "\n\n________________________________________________________________________________________________________________________\nEnter the desired unit class of measurement desired for current temperature:\n\t- Typing 'standard', 'default' or 'any word != imperial or metric' induces the selection of Standard (Kelvin)\n\t- Typing 'imperial' induces the selection of Imperial (Fahrenheit)\n\t- Typing 'metric' induces the selection of Metric (Celsius)\n________________________________________________________________________________________________________________________\n"
# \n
# \n________________________________________________________________________________________________________________________
# \nEnter the desired unit class of measurement desired for current temperature:
# \n\t- Typing 'standard', 'default' or 'any word != imperial or metric' induces the selection of Standard (Kelvin)
# \n\t- Typing 'imperial' induces the selection of Imperial (Fahrenheit)
# \n\t- Typing 'metric' induces the selection of Metric (Celsius)
# \n________________________________________________________________________________________________________________________
# \n
@units_input = gets.strip.to_s.downcase
end
end
如何测试实例变量@units\u input
是否通过gets.strip.to\u.downcase
设置为用户输入?我遇到了麻烦。我找不到答案
describe 'units_inputs' do
let(:units_input) { double("metric") }
let(:unit) { CLIinterface.new}
it "once" do
# have and_return return the stub, not the symbol
expect(unit).to receive(:gets).and_return(units_input).once
# call the method
unit.units_inputs
# check that the instance variable is set
expect(unit.instance_method_get(:@units_input)).to eq(units_input)
end
我曾尝试过仿效趣味网站,但上面提到:
Failures:
1) units_inputs once
Failure/Error: unit.units_inputs
# <Double "metric"> received unexpected message :strip with (no args)
# ./cli.rb:14:in `units_inputs'
# ./spec/cli_spec.rb:11:in `block (2 levels) in <top (required)>'
故障:
1) 每输入一次
故障/错误:unit.units\u输入
#收到意外消息:带(无参数)的条带
#./cli.rb:14:“单位输入”
#./spec/cli_spec.rb:11:in“块(2层)in”
如下修改您的规范:
describe 'units_inputs' do
let(:units_input) { double("metric") }
let(:unit) { CLIinterface.new}
it "once"
# have and_return return the stub, not the symbol
expect(unit).to receive(:gets).and_return(units_input).once
# call the method
unit.units_inputs
# check that the instance variable is set
expect(unit.instance_method_get(:@units_input)).to eq(units_input)
end
end
实际上你不需要双打。有时对原语进行操作是非常好的(特别是因为您需要double来符合多个字符串方法) 我会这样写你的规格:
describe 'units_inputs' do
let(:units_input) { 'Imperial ' } # mixed case, some spaces
let(:unit) { CLIinterface.new }
it "once" do
expect(unit).to receive(:gets).and_return(units_input).once
unit.units_inputs
expect(unit.instance_variable_get(:@units_input)).to eq('imperial') # downcased, spaces stripped
end
end
再想一想,这里甚至不需要instance\u variable\u get
,因为当前代码将@units\u input
作为其隐式返回值返回。可进一步简化:
describe 'units_inputs' do
let(:units_input) { 'Imperial ' } # mixed case, some spaces
let(:unit) { CLIinterface.new }
it "once" do
expect(unit).to receive(:gets).and_return(units_input).once
result = unit.units_inputs
expect(result).to eq('imperial') # downcased, spaces stripped
end
end
您甚至可以内联unit.units\u input
,这样也可以工作,但看起来有点奇怪。代码完全是期望值,没有明显的“肉”
你的问题是什么?对不起,它没有保存我之前键入的所有文本。我重新添加了我的陈述/问题。感谢您指出我的缺陷。您是否正在寻找一种在Rspec中模拟用户输入的方法?您是否可以偶然提供一个示例?就像我说的,我已经尝试并阅读了很多东西,所以我想知道我做了什么/尝试了什么,你可能会提出建议。?我不确定当当前温度没有在代码中显示时如何实现这一点?
故障:1)单元输入一次故障/错误:unit.units\u输入收到意外消息:带(无参数)#/cli.rb:14:in'units\u inputs'#/spec/cli\u spec.rb:11:in'块(2级)在
中,如果你在这里,我会吻你。。。。非常感谢你!非常有用,感谢您在最终压缩版本之前对整个过程进行的简化/抽象。
expect(unit).to receive(:gets).and_return(units_input).once
expect(unit.units_inputs).to eq('imperial') # downcased, spaces stripped