RSpec -- 测试方法是否调用了它的块参数 [英] RSpec -- test if method called its block parameter
问题描述
我有一个将代码块作为参数的方法.问题是:如果这个方法调用了块,如何使用 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?
块可以在方法需要的任何范围内进行评估,不一定使用 yield
或 block.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屋!