Successfully reported this slideshow.
Your SlideShare is downloading. ×

Fake your files - MemFs

Ad

Fake your files
Simon Courtois - @happynoff

Ad

How to test files
manipulation ?

Ad

class FileMaker
def create(path)
FileUtils.touch(path)
end
end

it "creates the given file" do
file_maker.create('thing.tx...

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Upcoming SlideShare
Python 101
Python 101
Loading in …3
×

Check these out next

1 of 24 Ad
1 of 24 Ad
Advertisement

More Related Content

Advertisement

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

×