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
和模拟的复杂情况.
See also this for more complex case with lets
and mocks.
推荐答案
我通常会做类似的事情
a = 1
b.go { a = 2}
a.should == 2
这篇关于RSpec-测试方法是否调用了其块参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!