我有一个函数,它接受一个块,打开一个文件,产生并返回:
def start &block
.....do some stuff
File.open("filename", "w") do |f|
f.write("something")
....do some more stuff
yield
end
end
我正在尝试使用 rspec 为它编写一个测试。如何存根 File.open 以便将对象 f (由我提供)传递给块,而不是尝试打开实际文件?就像是:
it "should test something" do
myobject = double("File", {'write' => true})
File.should_receive(:open).with(&blk) do |myobject|
f.should_receive(:write)
blk.should_receive(:yield) (or somethig like that)
end
end