【发布时间】:2017-05-12 22:58:23
【问题描述】:
我在为以下场景编写集成(无存根)测试时遇到困难:在循环中运行的进程(rake 任务)发出一些值。下面是用例的近似值。
如果我控制-C,测试将成功,但我希望它捕捉成功条件并停止。
有人有什么好的建议吗? (存根/嘲笑不是好的建议)。我想可能有办法在匹配器返回成功后指示 RSpec 停止进程?
describe 'rake reactor' do
it 'eventually returns 0.3' do
expect { Rake::Task['reactor'].execute }.to output(/^0\.3.*/).to_stdout
end
end
class Reactor
def initialize
@stop = false
end
def call
loop do
break if stop?
sleep random_interval
yield random_interval
end
end
def stop
@stop = true
end
def stop?
@stop == true
end
def random_interval
rand(0.1..0.4)
end
end
desc 'Start reactor'
task reactor: :environment do
reactor = Reactor.new
trap(:INT) do
reactor.stop
end
reactor.call { |m| p m }
end
【问题讨论】: