RSpec -- 测试方法是否调用了它的块参数 [英] RSpec -- test if method called its block parameter

查看:18
本文介绍了RSpec -- 测试方法是否调用了它的块参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将代码块作为参数的方法.问题是:如果这个方法调用了块,如何使用 RSpec 进行测试?

I have a method that takes block of code as an argument. The problem is: how to test using RSpec if this method called the block?

块可以在方法需要的任何范围内进行评估,不一定使用 yieldblock.call.它被传递给另一个类,或者在匿名类对象或其他地方对其进行评估.为了让测试通过,作为方法调用的结果在某处评估块就足够了.

The block may be evaluated in any scope the method needs, not necessarily using a yield or block.call. It be passed to another class, or evaluated it in an anonymous class object or somewhere else. For the test to pass it is enough to evaluate the block somewhere as a result of the method call.

有没有办法使用 RSpec 来测试这样的东西?

Is there a way to test something like this using RSpec?

另见这个了解更多lets 和 mocks 的复杂案例.

See also this for more complex case with lets and mocks.

推荐答案

我通常会做类似的事情

a = 1
b.go { a = 2}
a.should == 2

这篇关于RSpec -- 测试方法是否调用了它的块参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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