Rspec获取条带测试将无法运行

时间:2017-10-30 14:15:34

标签: ruby rspec

我正在尝试规范当前的方法:

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_input测试实例变量gets.strip.to_s.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)>'

2 个答案:

答案 0 :(得分:0)

你实际上并不需要双打。有时候对基元进行操作是完全没问题的(特别是因为你需要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_variable_get,因为当前代码返回@units_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_input并且它可以工作,但它看起来有点奇怪。代码是所有的期望,没有明显的“肉”。

expect(unit).to receive(:gets).and_return(units_input).once
expect(unit.units_inputs).to eq('imperial') # downcased, spaces stripped

答案 1 :(得分:-1)

像这样修改您的规范:

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