Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test that a block was yielded to?

Tags:

ruby

rspec

I have a test which needs to check if a block given to a method is being called.

block = lambda { 
    #some stuff 
}
block.should_receive(:call)

get_data_with_timeout(1, &block)

def get_data_with_timeout(timeout)
    begin
        timeout(timeout) {
            data = get_data
            yield data #do stuff
        }
    rescue Timeout::Error
        #timeout!
    end
end

Essentially I want to check that if there is no timeout then the block is being called and visa versa. Is this possible in rspec?

like image 457
roo Avatar asked Dec 11 '25 07:12

roo


1 Answers

A common pattern that I use:

block_called = false
get_data_with_timeout(1) do
    block_called = true
end
block_called.should be_true
like image 89
Hongli Avatar answered Dec 13 '25 00:12

Hongli



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!