开发者

How to test that a block was yielded to?

开发者 https://www.devze.com 2022-12-20 07:22 出处:网络
I have a test which needs to check if a block given to a method is being called. block = lambda { #some stuff

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, &开发者_运维百科amp;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?


A common pattern that I use:

block_called = false
get_data_with_timeout(1) do
    block_called = true
end
block_called.should be_true
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号