我正在使用 rspec 在 Ruby 中开发一些测试用例。
我正在尝试模拟 popen3 函数。
但是,在仍然保持阻塞形式的同时,我无法捕获预期的输出信息:
Class MyClass
def execute_command
Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
output['wait_thr'] = wait_thr.value
while line = stderr.gets
output['stderr'] += line
end
end
return output
end
end
为了模拟该功能,我正在执行以下操作:
it 'should do something'
response = []
response << 'stdin'
response << 'stdout'
response << 'test'
response << 'exit 0'
# expect
allow(Open3).to receive(:popen3).with(command).and_yield(response)
# when
output = myClassInstance.execute_script
#then
expect(output['wait_thr'].to_s).to include('exit 0')
模拟该函数不会输入“do”代码,我只剩下一个空数据结构。
我想知道我怎样才能正确地做到这一点?
谢谢!