Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I mock/fake the existence of a file using rspec?

This is what I have:

it "should be able to get a valid directory path" do  
   @asset.some_file_path.should == "/something/test.jpg"
end

The problem is that some_file_path returns "/not_here_yet.jpg" if there is no existing file.

def some_file_path
  if File.exists(self.my_image_path)
    return my_image_path
  else
    return "/not_here_yet.jpg
  end
end

I don't really want to create a new file in my tests. Is there a way for me to fake the existence of the file?

I'm thinking something in the lines of :

it "should be able to get a valid directory path" do  
   AwesomeFakeFileCreator.create(@asset.my_image_path)
   @asset.some_file_path.should == "/something/test.jpg"
end

Is this possible? How can I do such a thing?

Edit: I looked a bit at FakeFS but I'm not sure it answers my question

like image 954
marcgg Avatar asked Feb 10 '10 10:02

marcgg


1 Answers

You could do something on the lines of

it "should be able to get a valid directory path" do  
  File.stub!(:exists?).and_return(true)
  @asset.stub!(:my_image_path).and_return("/something/test.jpg")
  @asset.some_file_path.should == "/something/test.jpg"
end
like image 114
nas Avatar answered Oct 18 '22 01:10

nas