如何模拟/存储这个以便我可以测试each_line中发生了什么?
File.open(file_path) do |file|
file.each_line do |line|
#do something here
end
end
答案 0 :(得分:2)
尝试这样的事情:
RSpec.describe "an example of mock" do
let(:content) { StringIO.new("1\n2\n3") }
specify do
allow(File).to receive(:open).and_yield(content)
# do whatever you want
end
end