2017-10-30 3 views
0

を実行しません取得します。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)>' 
+1

ご質問はありますか? – sawa

+2

申し訳ありませんが、前に入力したすべてのテキストは保存されませんでした。私は私が持っているステートメント/質問を再追加しました。私の欠陥を指摘していただきありがとうございます。 – bRRRITSCOLD

+0

Rspecでユーザーの入力を模倣する方法を探していますか? – sawa

答えて

0

あなたが実際にダブルスを必要としませんここに。時にはプリミティブを操作するのはまったく問題ありません(特に、複数の文字列メソッドに合わせるには2倍が必要です)。

私はこのようにあなたのスペックを書きたい:第二思想に

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 

、現在のコードは、その暗黙の戻り値として@units_inputを返すよう、あなたも、ここinstance_variable_getを必要としません。さらに簡素化が可能です:

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

ここにいたら私はあなたにキスします....ありがとうございました!非常に便利であり、最終縮約版までの簡素化/抽象化に感謝します。 – bRRRITSCOLD

-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 
+0

'失敗unit.units_inputsは #<ダブル "メトリックは">予期しないメッセージを受信:(なし引数付きストリップ) 'をunits_inputs' ./cli.rb:14:in # #./spec/cli_spec.rb:11:in 'トップ(必須)のブロック(2レベル)' – bRRRITSCOLD

関連する問題