Know Your Diamonds - Spot The Fake To Avoid Wasting Bond Loans
Fake your files - MemFs
-
Upload
simon-courtois -
Category
Technology
-
view
341 -
download
0
description
Transcript of Fake your files - MemFs
Fake your filesSimon Courtois - @happynoff
How to test files manipulation ?
it "creates the given file" do
endfile_maker.create('thing.txt')
class FileMaker def create(path) FileUtils.touch(path) end end
it "creates the given file" do
endexpect(File.exists?(‘thing.txt’)).to be_truefile_maker.create('thing.txt')
class FileMaker def create(path) FileUtils.touch(path) end end
class FileMaker def create(path) FileUtils.touch(path) end end
it "creates the given file" do
end
file_maker.create('thing.txt')FileUtils.rm('thing.txt')
expect(File.exists?(‘thing.txt’)).to be_true
Read-only file system ?
it "creates the given file" do
endfile_maker.create('thing.txt')
class FileMaker def create(path) FileUtils.touch(path) end end
class FileMaker def create(path) FileUtils.touch(path) end end
it "creates the given file" do
end
expect(FileUtils).to receive(:touch) .with(‘thing.txt') .and_return(true)file_maker.create('thing.txt')
Boooh! Test behavior not implementation
Enters a solution FakeFS
it "creates the given file" dofile_maker.create('thing.txt')
end expect(File.exists?('thing.txt')).to be_true
class FileMaker def create(path) FileUtils.touch(path end end
)
class FileMaker def create(path) FileUtils.touch(path end end
it "creates the given file" doFakeFS do
file_maker.create('thing.txt')expect(File.exists?('thing.txt')).to
endend
be_true
)
class FileMaker def create(path) FileUtils.touch(path end end
it "creates the given file" doFakeFS do
file_maker.create('thing.txt')expect(File.exists?('thing.txt')).to
endend
be_false
, noop: true)
be_true
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" dofile_maker.create('thing.txt')expect(File.exists?('thing.txt')).to be_false
end
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
endend
MemFs.activate do
MemFs doesn’t overwrite FileUtils
only low-level classes
File.open('thing.txt') do |f| f.puts 'hello' end !File.read('thing.txt') # => "hello\n"
File.symlink('thing.txt', 'thing-link.txt') File.symlink?('thing-link.txt') # => true
File.chmod(0777, 'thing.txt')
File.stat('thing.txt').ctime # => 2014-02-04 19:00:00 +0100
Some resources
http://github.com/defunkt/fakefs
http://github.com/simonc/memfs
Questions?
Thank youSimon Courtois - @happynoff