Mocking calls to FileUtils or File means tightly coupling tests with the implementation.
it "creates a directory" do
FileUtils.expects(:mkdir).with("directory").once
Library.add "directory"
endThe above test will break if mkdir_p is used instead.
Refactoring code should not necessitate refactoring tests.
A better approach is to use a temp directory if you are working with relative directories.
require 'tmpdir'
it "creates a directory" do
Dir.mktmpdir do |dir|
Dir.chdir dir do
Library.add "directory"
assert File.directory?("directory")
end
end
endBut if you are working with absolute directories or do not want to use temporary directories, use FakeFS instead:
it "creates a directory" do
FakeFS do
Library.add "directory"
assert File.directory?("directory")
end
endInstallation
gem install fakefsUsage
To fake out the FS:
require 'fakefs'Temporarily faking the FS
require 'fakefs/safe'
FakeFS.activate!
# your code
FakeFS.deactivate!
# or
FakeFS do
# your code
endMocking IO methods
The IO class is the very basis for all Input and Output in Ruby, not only simple File reading/writting operations. To avoid breaking critical components, the fakefs gem is not mocking IO methods by default.
However you can enable some very simple mocks using an explicit optin keyword:
require 'fakefs/safe'
FakeFS.activate!(io_mocks: true)
# your code
FakeFS.deactivate!Rails
In rails projects, add this to your Gemfile:
gem "fakefs", require: "fakefs/safe"RSpec
Include FakeFS::SpecHelpers to turn FakeFS on and off in an example group:
require 'fakefs/spec_helpers'
describe "my spec" do
include FakeFS::SpecHelpers
endSee lib/fakefs/spec_helpers.rb for more info.
To use FakeFS within a single test and be guaranteed a fresh fake filesystem:
require 'fakefs/safe'
describe "my spec" do
context "my context" do
it "does something to the filesystem"
FakeFS.with_fresh do
# whatever it does
end
end
end
endFakeFs --- TypeError: superclass mismatch for class File
pp and fakefs may collide, even if you're not actually explicitly using pp. Adding require 'pp' before require 'fakefs' should fix the problem locally. For a module-level fix, try adding it to the Gemfile:
source "https://rubygems.org"
require 'pp'
# list of gemsThe problem may not be limited to pp; any gems that add to File may be affected.
Working with existing files
Clone existing directories or files to reuse them during tests, they are safe to modify.
FakeFS do
config = File.expand_path('../../config', __FILE__)
FakeFS::FileSystem.clone(config)
expect(File.read("#{config}/foo.yml")).to include("original-content-of-foo")
File.write("#{config}/foo.yml", "NEW")
expect(File.read("#{config}/foo.yml")).to eq "NEW"
endIntegrating with other filesystem libraries
Third-party libraries may add methods to filesystem-related classes. FakeFS
doesn't support these methods out of the box, but you can define fake versions
yourself on the equivalent FakeFS classes. For example,
FileMagic adds File#content_type.
A fake version can be provided as follows:
FakeFS::File.class_eval do
def content_type
'fake/file'
end
endFile.lock
Warning: experimental and might break if you obtain more that one flock per file using different descriptors
require 'fakefs/flockable_file'Caveats
FakeFS internally uses the Pathname and FileUtils constants. If you use
these in your app, be certain you're properly requiring them and not counting
on FakeFS' own require.
As of v0.5.0, FakeFS's current working directory (i.e. Dir.pwd) is
independent of the real working directory. Previously if the real working
directory were, for example, /Users/donovan/Desktop, then FakeFS would use
that as the fake working directory too, even though it most likely didn't
exist. This caused all kinds of subtle bugs. Now the default working directory
is the only thing that is guaranteed to exist, namely the root (i.e. /). This
may be important when upgrading from v0.4.x to v0.5.x, especially if you depend
on the real working directory while using FakeFS.
FakeFS replaces File and FileUtils, but is not a filesystem replacement, so gems
that use strange commands or C might circumvent it. For example, the sqlite3
gem will completely ignore any faked filesystem.
Speed?
Contributing
Once you've made your great commits:
- Fork FakeFS
- Create a topic branch -
git checkout -b my_branch - Push to your branch -
git push origin my_branch - Open a Pull Request
- That's it!
Meta
- Code:
git clone git://github.com/fakefs/fakefs.git - Home: https://github.com/fakefs/fakefs
- Bugs: https://github.com/fakefs/fakefs/issues
- Test: https://github.com/fakefs/fakefs/actions?query=branch%3Amaster
- Gems: https://rubygems.org/gems/fakefs
Releasing
-
bundle exec rake bump:patchor minor/major bundle exec rake release