views:

128

answers:

3

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

+3  A: 

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
nas
Could you detail a bit what this is doing?
marcgg
The first line is stubbing out that the file exists returns true and the second line is stubbing out my_image_path method on asset object and returns the file path, which your some_file_path method will return in that case, i.e. which the whole spec example is about. In summary, return the path if it exists.
nas
Not that you have to keep me happy or anything -- I didn't ask the question -- but I'm afraid that I didn't understand that.
Shadowfirebird
@Shadowfirebird If you point out the bit that you didn't understand then I will be able to address the issue more specifically :)
nas
Assuming my_image_path is simply a string attribute, there's no reason to stub my_image_path. Simply stubbing File.exists should do the trick.
Dave Sims
@Dave Sims True if that's the case
nas
A: 

This is probably a highly unhelpful answer as I am only just getting my head around automated testing, myself, but:

As I understand it, you only need to test two conditions: file exists, and file does not exist?

Testing non-existence is easy enough: create an Asset with a my_image_path that doesn't exist, and check the function returns '/not_here_yet.jpg'.

Surely you can test the other case in the same way? create an empty file with a .jpg extension, then an Asset that points to it, and check that the function returns the correct name?

Shadowfirebird
I don't want to create files on the fly. Tests would be slower and I would have to be extra carefull creating/deleting the file.
marcgg
:) I did say it would probably be an unhelpful answer...
Shadowfirebird
A: 

Maybe you can fake with the correct path of your File. Fake for each file that you want to return the path

  it "should be able to get a valid directory path" do  
    File.stub!(:exists?).with("my_image_path.jpg").and_return("my_image_path.jpg")
    @asset.some_file_path.should == "my_image_path.jpg"
  end
Boris Barroso