Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Fake your files
Simon Courtois - @happynoff
How to test files
manipulation ?
class FileMaker
def create(path)
FileUtils.touch(path)
end
end

it "creates the given file" do
file_maker.create('thing.tx...
class FileMaker
def create(path)
FileUtils.touch(path)
end
end

it "creates the given file" do
file_maker.create('thing.tx...
class FileMaker
def create(path)
FileUtils.touch(path)
end
end

it "creates the given file" do
FileUtils.rm('thing.txt')
f...
Read-only file system ?
class FileMaker
def create(path)
FileUtils.touch(path)
end
end

it "creates the given file" do
file_maker.create('thing.tx...
class FileMaker
def create(path)
FileUtils.touch(path)
end
end

it "creates the given file" do
expect(FileUtils).to receiv...
Boooh!
Test behavior not
implementation
Enters a solution
FakeFS
class FileMaker
def create(path)
FileUtils.touch(path)
end
end

it "creates the given file" do
file_maker.create('thing.tx...
class FileMaker
def create(path)
FileUtils.touch(path)
end
end

it "creates the given file" do
FakeFS do
file_maker.create...
class FileMaker
def create(path)
FileUtils.touch(path, noop: true)
end
end

it "creates the given file" do
FakeFS do
file_...
BOOM!
That’s a red dot
FakeFS
overwrites FileUtils and
ignores options
Try MemFS
class FileMaker
def create(path)
FileUtils.touch(path, noop: true)
end
end

it "creates the given file" do
file_maker.crea...
class FileMaker
def create(path)
FileUtils.touch(path, noop: true)
end
end

it "creates the given file" do
MemFs.activate ...
MemFs
doesn’t overwrite FileUtils
only low-level classes
File.open('thing.txt') do |f|
f.puts 'hello'
end

!

File.read('thing.txt')
# => "hellon"

File.symlink('thing.txt', 'thin...
Some resources
http://github.com/defunkt/fakefs
http://github.com/simonc/memfs
Questions

?
Thank you
Simon Courtois - @happynoff
Upcoming SlideShare
Loading in …5
×

Fake your files - MemFs

837 views

Published on

My talk for Paris.rb on 2014-02-04.

Published in: Technology
  • Be the first to comment

Fake your files - MemFs

  1. 1. Fake your files Simon Courtois - @happynoff
  2. 2. How to test files manipulation ?
  3. 3. class FileMaker def create(path) FileUtils.touch(path) end end it "creates the given file" do file_maker.create('thing.txt') end
  4. 4. class FileMaker def create(path) FileUtils.touch(path) end end it "creates the given file" do file_maker.create('thing.txt') expect(File.exists?(‘thing.txt’)).to be_true end
  5. 5. class FileMaker def create(path) FileUtils.touch(path) end end it "creates the given file" do FileUtils.rm('thing.txt') file_maker.create('thing.txt') expect(File.exists?(‘thing.txt’)).to be_true end
  6. 6. Read-only file system ?
  7. 7. class FileMaker def create(path) FileUtils.touch(path) end end it "creates the given file" do file_maker.create('thing.txt') end
  8. 8. class FileMaker def create(path) FileUtils.touch(path) end end it "creates the given file" do expect(FileUtils).to receive(:touch) .with(‘thing.txt') .and_return(true) file_maker.create('thing.txt') end
  9. 9. Boooh! Test behavior not implementation
  10. 10. Enters a solution FakeFS
  11. 11. class FileMaker def create(path) FileUtils.touch(path) end end it "creates the given file" do file_maker.create('thing.txt') expect(File.exists?('thing.txt')).to be_true end
  12. 12. class FileMaker def create(path) FileUtils.touch(path) end end it "creates the given file" do FakeFS do file_maker.create('thing.txt') expect(File.exists?('thing.txt')).to be_true end end
  13. 13. class FileMaker def create(path) FileUtils.touch(path, noop: true) end end it "creates the given file" do FakeFS do file_maker.create('thing.txt') expect(File.exists?('thing.txt')).to be_true be_false end end
  14. 14. BOOM! That’s a red dot
  15. 15. FakeFS overwrites FileUtils and ignores options
  16. 16. Try MemFS
  17. 17. class FileMaker def create(path) FileUtils.touch(path, noop: true) end end it "creates the given file" do file_maker.create('thing.txt') expect(File.exists?('thing.txt')).to be_false end
  18. 18. class FileMaker def create(path) FileUtils.touch(path, noop: true) end end it "creates the given file" do MemFs.activate do file_maker.create('thing.txt') expect(File.exists?('thing.txt')).to be_false end end
  19. 19. MemFs doesn’t overwrite FileUtils only low-level classes
  20. 20. File.open('thing.txt') do |f| f.puts 'hello' end ! File.read('thing.txt') # => "hellon" File.symlink('thing.txt', 'thing-link.txt') File.symlink?('thing-link.txt') # => true File.stat('thing.txt').ctime # => 2014-02-04 19:00:00 +0100 File.chmod(0777, 'thing.txt')
  21. 21. Some resources
  22. 22. http://github.com/defunkt/fakefs http://github.com/simonc/memfs
  23. 23. Questions ?
  24. 24. Thank you Simon Courtois - @happynoff

×