Reputation: 571
I want to store the code will be run inside each_with_index
block:
describe '#each_with_index' do
subject { [:a,:b].each_with_index{ block.to_ruby } }
context 'when the block prints v to console' do
let(:block) { '|v| puts v' }
specify { expect { subject }.to output(':a :b').to_stdout }
end
context 'when the block prints i to console' do
let(:block) { '|v,i| puts i' }
specify { expect { subject }.to output('0 1').to_stdout }
end
context 'when the block prints v and i to console' do
let(:block) { '|v,i| puts "#{v} and {i}"' }
specify { expect { subject }.to output(':a and 0 :b and 1').to_stdout }
end
end
I don't need the code stored as a string, it is just a way to show you what I mean. I want to do this with in-block code, pipes, and everything. I've got a feeling we can use Proc.new
but the pipes are tripping me up.
Upvotes: 2
Views: 212
Reputation: 4478
Something like:
let(:block) { Proc.new{ |v| puts v } }
subject { [:a,:b].each_with_index { |*args| block.call args } }
Upvotes: 3