How to check retrieving a list of files in a directory using RSpec?

I am new to the RSpec world. I am writing RubyGem, which deals with the list of files in the specified directory and in any subdirectories. In particular, it will use Find.findand add files to the array for subsequent output.

I would like to write a specification to test this behavior, but I don’t know where to start from the point of view of falsifying file directory and stubbing Find.findetc. This is something that is still not enough for me:

it "should return a list of files within the specified directory" do
end

Any help is much appreciated!

+2
source share
2 answers

I don't think you need to test the library, but if you have a method like

def file_names
  files = []
  Find.find(Dir.pwd){|file| files << file}
  ........
end

find,

it "should return a list of files within the specified directory" do
  Find.stub!(:find).and_return(['file1', 'file2'])
  @object.file_names
end

,

it "should return a list of files within the specified directory" do
  Find.should_receive(:find).and_return(['file1', 'file2'])
  @object.file_names
end
+2

fakeFS gem.

+2

Source: https://habr.com/ru/post/1748541/


All Articles