Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing a method with yield using rspec

I have a method which yields. I am testing it for two conditions.

1) if it yields ( I am successfully able to do this using the yield match)

2) if it does not yield. ( This is where any help in the right direction would be appreciated.)

Method:

def example_method
  yield if block_given?
end

Specs:

it 'yields to a block' do
  expect{ |b| example_method(&b) }.to yield_with_no_args
end

it 'does not yield if a block is not given' do
end

The first spec passes successfully, but for the second spec I am not sure how to proceed, since the yield match would require a block to be given and if a block is given it would pass, whereas I want to test it for not yielding if a block is not given.

Thanks!

like image 708
Jaleel Ahmed Avatar asked Aug 13 '26 15:08

Jaleel Ahmed


1 Answers

Try something like

expect { example_method }.to_not yield_control
like image 152
Ursus Avatar answered Aug 16 '26 20:08

Ursus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!