2009-05-29 18:24:42 +00:00
|
|
|
FakeFS
|
|
|
|
======
|
|
|
|
|
|
|
|
Mocha is great. But when your library is all about manipulating the
|
|
|
|
filesystem, you really want to test the behavior and not the implementation.
|
|
|
|
|
|
|
|
If you're mocking and stubbing every call to FileUtils or File, you're
|
|
|
|
tightly coupling your tests with the implementation.
|
|
|
|
|
|
|
|
def test_creates_directory
|
|
|
|
FileUtils.expects(:mkdir).with("directory").once
|
|
|
|
Library.add "directory"
|
|
|
|
end
|
|
|
|
|
|
|
|
The above test will break if we decide to use `mkdir_p` in our code. Refactoring
|
|
|
|
code shouldn't necessitate refactoring tests.
|
|
|
|
|
|
|
|
With FakeFS:
|
|
|
|
|
|
|
|
def test_creates_directory
|
|
|
|
Library.add "directory"
|
|
|
|
assert File.directory?("directory")
|
|
|
|
end
|
|
|
|
|
|
|
|
Woot.
|
|
|
|
|
2009-07-27 06:56:51 +00:00
|
|
|
|
|
|
|
Usage
|
|
|
|
-----
|
|
|
|
|
2009-07-27 06:58:46 +00:00
|
|
|
require 'fakefs'
|
2009-07-27 06:56:51 +00:00
|
|
|
|
2009-07-27 06:58:46 +00:00
|
|
|
# That's it.
|
2009-07-27 06:56:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
Don't Fake the FS Immediately
|
|
|
|
-----------------------------
|
|
|
|
|
2009-07-27 06:58:46 +00:00
|
|
|
require 'fakefs/safe'
|
|
|
|
|
|
|
|
FakeFS.activate!
|
|
|
|
# your code
|
2009-07-27 06:59:54 +00:00
|
|
|
FakeFS.deactivate!
|
2009-07-27 06:58:46 +00:00
|
|
|
|
|
|
|
# or
|
|
|
|
FakeFS do
|
|
|
|
# your code
|
|
|
|
end
|
2009-07-27 06:56:51 +00:00
|
|
|
|
|
|
|
|
2009-05-29 18:24:42 +00:00
|
|
|
How is this different than MockFS?
|
|
|
|
----------------------------------
|
|
|
|
|
|
|
|
FakeFS provides a test suite and works with symlinks. It's also strictly a
|
|
|
|
test-time dependency: your actual library does not need to use or know about
|
|
|
|
FakeFS.
|
|
|
|
|
2009-07-20 14:01:48 +00:00
|
|
|
|
|
|
|
Speed?
|
|
|
|
------
|
|
|
|
http://gist.github.com/150348
|
|
|
|
|
|
|
|
|
2009-05-29 18:24:42 +00:00
|
|
|
Authors
|
|
|
|
-------
|
|
|
|
|
|
|
|
Chris Wanstrath [chris@ozmm.org]
|
2009-07-27 06:54:45 +00:00
|
|
|
Pat Nakajima [http://github.com/nakajima]
|