Reputation: 1945
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?
Upvotes: 1
Views: 5337
Reputation: 1722
Actually
Mocking calls to FileUtils or File means tightly coupling tests with the implementation.
Just calling the following code before your stub should be enough.
allow(File).to receive(:exist?).and_call_original
Upvotes: 0
Reputation: 37409
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.
Upvotes: 3