如何模拟数组的排序期望 lambda 表达式?
这是我的问题的一个简单示例:
# initializing the data
l = lambda { |a,b| a <=> b }
array = [ 1, 2, 3, 4, 5 ]
sorted_array = [ 2, 3, 8, 9, 1]
# I expect that sort will be called using the lambda as a parameter
array.expects(:sort).with( l ).returns( sorted_array )
# perform the sort using the lambda expression
temp = array.sort{|a,b| l.call(a,b) }
现在,起初我预计这会奏效;但是,我收到以下错误:
- expected exactly once, not yet invoked: [ 1, 2, 3, 4, 5 ].sort(#<Proc:0xb665eb48>)
我意识到这不起作用,因为 l 没有作为参数传递给 l。但是,是否有另一种方法可以完成此代码试图完成的任务?
注意:我已经弄清楚了如何解决我的问题,而没有弄清楚如何执行上述操作。我会保持打开状态,以防其他人遇到类似问题。
干杯,约瑟夫