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」コードは入力されず、空のデータ構造が残ります。
どうすればこれを適切に行うことができるのだろうかと思っていました。
ありがとう!