際際滷

際際滷Share a Scribd company logo
Fake your 鍖les
Simon Courtois - @happynoff
How to test 鍖les
manipulation ?
class FileMaker
def create(path)
FileUtils.touch(path)
end
end

it "creates the given file" do
file_maker.create('thing.txt')
end
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
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
Read-only 鍖le system ?
class FileMaker
def create(path)
FileUtils.touch(path)
end
end

it "creates the given file" do
file_maker.create('thing.txt')
end
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
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.txt')
expect(File.exists?('thing.txt')).to be_true
end
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
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
BOOM!
Thats 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.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
MemFs.activate do
file_maker.create('thing.txt')
expect(File.exists?('thing.txt')).to be_false
end
end
MemFs
doesnt 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', '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')
Some resources
http://github.com/defunkt/fakefs
http://github.com/simonc/memfs
Questions

?
Thank you
Simon Courtois - @happynoff
Ad

Recommended

Python 101
Python 101
The Active Network
Introduction to JavaScript: Week Two
Introduction to JavaScript: Week Two
Event Handler
Getting to know Arel
Getting to know Arel
Ray Zane
SolrMeter Lightning talk - Lucene Revolution 2010
SolrMeter Lightning talk - Lucene Revolution 2010
Tom叩s Fern叩ndez L旦bbe
gitfs
gitfs
Temian Vlad
F strings
F strings
Mariatta Wijaya
Baabtra.com little coder chapter - 3
Baabtra.com little coder chapter - 3
baabtra.com - No. 1 supplier of quality freshers
10 reasons to love CoffeeScript
10 reasons to love CoffeeScript
Lukas Alexandre
DIWE - File handling with PHP
DIWE - File handling with PHP
Rasan Samarasinghe
Description 1) Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docx
theodorelove43763
Php files
Php files
kalyani66
File Handling in C Programming
File Handling in C Programming
RavindraSalunke3
Filesystems Lisbon 2018
Filesystems Lisbon 2018
Frank de Jonge
File handling in C++
File handling in C++
Hitesh Kumar
Ch3(working with file)
Ch3(working with file)
Chhom Karath
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3
Gheyath M. Othman
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
sangeeta borde
Log into your netlab workstation then ssh to server.cnt1015.local wi.docx
Log into your netlab workstation then ssh to server.cnt1015.local wi.docx
desteinbrook
Having Fun Programming!
Having Fun Programming!
Aaron Patterson
Automate the boring stuff with python
Automate the boring stuff with python
DEEPAKSINGHBIST1
Java File I/O
Java File I/O
Canterbury HS
Filing system in PHP
Filing system in PHP
Mudasir Syed
Linux Command Line - By Ranjan Raja
Linux Command Line - By Ranjan Raja
Ranjan Raja
Java 7 Features and Enhancements
Java 7 Features and Enhancements
Gagan Agrawal
Can someone put this code in a zip file. I tried running it last tim.pdf
Can someone put this code in a zip file. I tried running it last tim.pdf
fedosys
file_handling_in_c.ppt
file_handling_in_c.ppt
yuvrajkeshri
General Functions
General Functions
BabuDevanandam
UNIT 5.pptxpython for engineering students
UNIT 5.pptxpython for engineering students
SabarigiriVason
Conseils pour un lancement Product Hunt r辿ussi
Simon Courtois
Organize your assets with Rails
Organize your assets with Rails
Simon Courtois

More Related Content

Similar to Fake your files - MemFs (20)

DIWE - File handling with PHP
DIWE - File handling with PHP
Rasan Samarasinghe
Description 1) Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docx
theodorelove43763
Php files
Php files
kalyani66
File Handling in C Programming
File Handling in C Programming
RavindraSalunke3
Filesystems Lisbon 2018
Filesystems Lisbon 2018
Frank de Jonge
File handling in C++
File handling in C++
Hitesh Kumar
Ch3(working with file)
Ch3(working with file)
Chhom Karath
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3
Gheyath M. Othman
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
sangeeta borde
Log into your netlab workstation then ssh to server.cnt1015.local wi.docx
Log into your netlab workstation then ssh to server.cnt1015.local wi.docx
desteinbrook
Having Fun Programming!
Having Fun Programming!
Aaron Patterson
Automate the boring stuff with python
Automate the boring stuff with python
DEEPAKSINGHBIST1
Java File I/O
Java File I/O
Canterbury HS
Filing system in PHP
Filing system in PHP
Mudasir Syed
Linux Command Line - By Ranjan Raja
Linux Command Line - By Ranjan Raja
Ranjan Raja
Java 7 Features and Enhancements
Java 7 Features and Enhancements
Gagan Agrawal
Can someone put this code in a zip file. I tried running it last tim.pdf
Can someone put this code in a zip file. I tried running it last tim.pdf
fedosys
file_handling_in_c.ppt
file_handling_in_c.ppt
yuvrajkeshri
General Functions
General Functions
BabuDevanandam
UNIT 5.pptxpython for engineering students
UNIT 5.pptxpython for engineering students
SabarigiriVason
DIWE - File handling with PHP
DIWE - File handling with PHP
Rasan Samarasinghe
Description 1) Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docx
theodorelove43763
Php files
Php files
kalyani66
File Handling in C Programming
File Handling in C Programming
RavindraSalunke3
Filesystems Lisbon 2018
Filesystems Lisbon 2018
Frank de Jonge
File handling in C++
File handling in C++
Hitesh Kumar
Ch3(working with file)
Ch3(working with file)
Chhom Karath
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3
Gheyath M. Othman
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
sangeeta borde
Log into your netlab workstation then ssh to server.cnt1015.local wi.docx
Log into your netlab workstation then ssh to server.cnt1015.local wi.docx
desteinbrook
Having Fun Programming!
Having Fun Programming!
Aaron Patterson
Automate the boring stuff with python
Automate the boring stuff with python
DEEPAKSINGHBIST1
Filing system in PHP
Filing system in PHP
Mudasir Syed
Linux Command Line - By Ranjan Raja
Linux Command Line - By Ranjan Raja
Ranjan Raja
Java 7 Features and Enhancements
Java 7 Features and Enhancements
Gagan Agrawal
Can someone put this code in a zip file. I tried running it last tim.pdf
Can someone put this code in a zip file. I tried running it last tim.pdf
fedosys
file_handling_in_c.ppt
file_handling_in_c.ppt
yuvrajkeshri
UNIT 5.pptxpython for engineering students
UNIT 5.pptxpython for engineering students
SabarigiriVason

More from Simon Courtois (16)

Conseils pour un lancement Product Hunt r辿ussi
Simon Courtois
Organize your assets with Rails
Organize your assets with Rails
Simon Courtois
Speed your Rails app creation with templates
Speed your Rails app creation with templates
Simon Courtois
Dependency sorting in Ruby with TSort
Dependency sorting in Ruby with TSort
Simon Courtois
How Unidecoder Transliterates UTF-8 to ASCII
How Unidecoder Transliterates UTF-8 to ASCII
Simon Courtois
Get Slim!
Get Slim!
Simon Courtois
Multi tenant/lang application with Ruby on Rails
Multi tenant/lang application with Ruby on Rails
Simon Courtois
Rails is like Burger King
Rails is like Burger King
Simon Courtois
REST with Her (and let Her take care of the REST)
REST with Her (and let Her take care of the REST)
Simon Courtois
Ruby and DCI
Ruby and DCI
Simon Courtois
Cells
Cells
Simon Courtois
Mustdown
Mustdown
Simon Courtois
Vos Regexps sont fausses !
Vos Regexps sont fausses !
Simon Courtois
Ariane
Ariane
Simon Courtois
Pourquoi Ruby on Rails 巽a d辿chire ?
Pourquoi Ruby on Rails 巽a d辿chire ?
Simon Courtois
Commander
Commander
Simon Courtois
Conseils pour un lancement Product Hunt r辿ussi
Simon Courtois
Organize your assets with Rails
Organize your assets with Rails
Simon Courtois
Speed your Rails app creation with templates
Speed your Rails app creation with templates
Simon Courtois
Dependency sorting in Ruby with TSort
Dependency sorting in Ruby with TSort
Simon Courtois
How Unidecoder Transliterates UTF-8 to ASCII
How Unidecoder Transliterates UTF-8 to ASCII
Simon Courtois
Multi tenant/lang application with Ruby on Rails
Multi tenant/lang application with Ruby on Rails
Simon Courtois
Rails is like Burger King
Rails is like Burger King
Simon Courtois
REST with Her (and let Her take care of the REST)
REST with Her (and let Her take care of the REST)
Simon Courtois
Vos Regexps sont fausses !
Vos Regexps sont fausses !
Simon Courtois
Pourquoi Ruby on Rails 巽a d辿chire ?
Pourquoi Ruby on Rails 巽a d辿chire ?
Simon Courtois
Ad

Recently uploaded (20)

Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
janeliewang985
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Josef Weingand
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
Cluster-Based Multi-Objective Metamorphic Test Case Pair Selection for Deep N...
janeliewang985
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Josef Weingand
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
Ad

Fake your files - MemFs