How to verify that a block has been assigned?

I have a test that needs to check if the block specified for the 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 called and vice versa. Is this possible in rspec?

+3
source share
1 answer

The general template that I use is:

block_called = false
get_data_with_timeout(1) do
    block_called = true
end
block_called.should be_true
+7
source

Source: https://habr.com/ru/post/1733301/


All Articles