Fake your files - MemFs

100 %
0 %
Information about Fake your files - MemFs

Published on February 4, 2014

Author: happynoff

Source: slideshare.net

Description

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

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.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 file 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! 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.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 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', '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

Add a comment

Related pages

simonc/memfs · GitHub - GitHub · Where software is built

memfs - MemFs provides a fake file system that can be ... simonc / memfs. ... you need to be able to test it without touching your hard drive. MemFs is ...
Read more

Fake your files - Simon Courtois · Paris.rb

Fake your files - Simon Courtois. ... Oui avec la gem MemFs ! Epita. plus d'un an. Retrouver ce projet sur Github Contacter le mainteneur (@happynoff) ...
Read more

Renaming FsFaker to MemFs · simonc/memfs@41e665b · GitHub

2 spec/fs_faker/fake/file/content_spec.rb → spec/memfs/fake/file/content_spec.rb @@ -1,6 +1,6 @@ require ' spec_helper '-module FsFaker + module MemFs ...
Read more

Code generation, compilation, and naming conventions in ...

Isolating Code Under Test with Microsoft Fakes Code generation, compilation, and naming conventions in Microsoft Fakes
Read more

Hamid Shahid's Weblog: Microsoft Fakes Framework

The files in the "Fakes" folder are only ... I am using Microsoft Fakes with VS ... unit-tests-using-microsoft-fakes.html) Thanks for your ...
Read more

memfs | RubyGems.org | your community gem host

MemFs provides a fake file system that can be used for tests. Strongly inspired by FakeFS.
Read more

Fake your Specs 2.4.3.5 Free Download - Software / Utilities

Fake your Specs is a program that allows you to type custom specifications, ... Huge fake file creator 1.0 With this small piece of software...
Read more

How to Use a Fake IP Address & Mask Yourself Online

However, if you are still worried and would like to surf the web anonymously, you will need to obtain a fake IP address to mask your online identity.
Read more

Fake Your Mood With AgencyGlass | TechBeat

A Japanese student has created AgencyGlass - a pair of glasses that fakes your emotions with random eye movements.
Read more

How to Fake Your Own Death: 11 Steps (with Pictures) - wikiHow

How to Fake Your Own Death. Whether you're running from the cops, running away from home or simply want to start your life again, sometimes in life you may ...
Read more