如何测试一个块是否被屈服? [英] How to test that a block was yielded to?

查看:48
本文介绍了如何测试一个块是否被屈服?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试需要检查是否正在调用提供给方法的块.

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

基本上我想检查一下,如果没有超时,则块正在被调用,反之亦然.这在 rspec 中可能吗?

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?

推荐答案

我常用的一种模式:

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

这篇关于如何测试一个块是否被屈服?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆