2016-07-07 6 views
0

私はファイルを解析するクラスSourceReaderを作成し、ファイル内の認識されたトークンの数に応じて何度も生成します。たとえば、私がfile1.txtを解析すると、値oneが1回だけ出力されます。もう1つの例は、file2.txtを解析する場合、最初に値がone、次に値がtwoのものが2回生成されます。引数をx回使用してメソッドをテストする方法は?

rspecを使用して正しくテストするにはどうすればよいですか?ここで私はこれまで持っているものです。

require './spec/spec_helper' 

describe SourceReader do 
    describe '#each_card' do 

    context "given file with one card" do 
     input_filename = './spec/data/file1_spec.txt' 
     it 'yields once, with arguments "one"' do 
     File.open(input_filename, 'r') do |file| 
      sut = SourceReader.new(file) 
      expect(sut.each_card).to yield_with_args('one') 
     end 
     end 
    end 

    context "given file with two cards" do 
     input_filename = './spec/data/file2_spec.txt' 
     it 'yields twice, with arguments "one", then "two"' do 
     # some codes 
     end 
    end 

    end 
end 

私は

答えて

1
arr = [] 
sut.each_card do |arg| 
    arr << arg 
end 
expect(arr).to eq ['one', 'two'] 
+0

ありがとうドキュメントに記載されていexpect { |b| object.action(&b) }.to yield_with_argsを実装する方法について混乱しています、それは私の問題を解決しました! –

関連する問題