Q:

rspec to stub several File.exists? calls

I have a function with several File.exists? calls:
def create(one_filename, other_filename)
    raise Error, "One error" if File.exists?(one_filename)
    raise Error, "Other error" unless File.exist?(other_filename)
    .....
end

But I don't know the way to stub the second error. The spec is something as:
it "should not create an existing file" do
    File.stub(:exists?).and_return(true)
    expect {
        subject.create('whatever')
    }.to raise_error("One error")
    end
it "should has to exists the other filename" do
    File.stub(:exists?).and_return(false)
    expect {
        subject.create('whatever.yaml')
    }.to raise_error("Other error")
end

For the second spec (it "should has to exists the other filename") the File exists? stub raises the first check.
What is the best way to spec the two raises?

A:

To return multiple values for multiple calls, simply do:
File.stub(:exists?).and_return(false, true)

Or, preferably, use the new syntax:
Allow(File).to receive(:exists?).and_return(false, true)

Another option would be to stub the method according to its input variables:
Allow(File).to receive(:exists?).with(one_filename).and_return(false)
Allow(File).to receive(:exists?).with(other_filename).and_return(true)

This has the added value of actually testing the behavior rather than the implementation.

