【发布时间】:2014-11-06 10:44:26
【问题描述】:
我需要测试如下方法
class Something
def test_method
count = 0
arr = []
max = 500
puts "test_method"
loop do
arr += count if count.even?
count += 1
end break if count > max
return arr
end
end
所以基本上我需要测试 test_method() 是否返回一个数组实例并且 arr 大小是否大于 3。 但我不想每次都进入这个循环并返回结果。那么在 rspec 中有什么方法可以存根最大值并返回数组而不循环 500 次。
【问题讨论】:
-
将
max作为参数传递。 -
我不想编辑编写的方法(test_method)。那么没有那个有什么办法吗??
-
不,不是这个方法的写法。
max是“硬编码”的,你不能存根局部变量。 -
顺便说一句,您的方法实际上并没有运行。
arr += count不起作用,break在您的loop之外