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.
|
|
|
|
|
2009-10-07 04:07:28 +00:00
|
|
|
If you're mocking and stubbing every call to FileUtils or File, you're
|
2009-05-29 18:24:42 +00:00
|
|
|
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'
|
2009-11-08 06:04:16 +00:00
|
|
|
|
2009-07-27 06:58:46 +00:00
|
|
|
FakeFS.activate!
|
|
|
|
# your code
|
2009-07-27 06:59:54 +00:00
|
|
|
FakeFS.deactivate!
|
2009-11-08 06:04:16 +00:00
|
|
|
|
2009-07-27 06:58:46 +00:00
|
|
|
# or
|
|
|
|
FakeFS do
|
|
|
|
# your code
|
|
|
|
end
|
2009-07-27 06:56:51 +00:00
|
|
|
|
2009-10-28 18:02:25 +00:00
|
|
|
|
2009-10-14 14:27:37 +00:00
|
|
|
RSpec
|
|
|
|
-----------------------------
|
|
|
|
The above approach works with RSpec as well. In addition to this you may use the
|
|
|
|
'use_fakefs' macro to turn FakeFS on and off in a given example group. See
|
|
|
|
lib/spec_helpers for more details on it's usage.
|
|
|
|
|
2009-07-27 06:56:51 +00:00
|
|
|
|
2009-05-29 18:24:42 +00:00
|
|
|
How is this different than MockFS?
|
|
|
|
----------------------------------
|
|
|
|
|
2009-10-07 04:07:28 +00:00
|
|
|
FakeFS provides a test suite and works with symlinks. It's also strictly a
|
2009-05-29 18:24:42 +00:00
|
|
|
test-time dependency: your actual library does not need to use or know about
|
|
|
|
FakeFS.
|
|
|
|
|
2009-07-20 14:01:48 +00:00
|
|
|
|
2009-10-28 18:02:25 +00:00
|
|
|
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.
|
|
|
|
|
|
|
|
|
2009-07-20 14:01:48 +00:00
|
|
|
Speed?
|
|
|
|
------
|
2009-10-07 04:07:28 +00:00
|
|
|
<http://gist.github.com/156091>
|
2009-07-20 14:01:48 +00:00
|
|
|
|
|
|
|
|
2009-10-07 04:07:28 +00:00
|
|
|
Installation
|
|
|
|
------------
|
2009-05-29 18:24:42 +00:00
|
|
|
|
2009-10-07 04:07:28 +00:00
|
|
|
### [Gemcutter](http://gemcutter.org/)
|
|
|
|
|
|
|
|
$ gem install fakefs
|
|
|
|
|
|
|
|
### [Rip](http://hellorip.com)
|
|
|
|
|
|
|
|
$ rip install git://github.com/defunkt/fakefs.git
|
|
|
|
|
|
|
|
|
|
|
|
Meta
|
|
|
|
----
|
|
|
|
|
|
|
|
* Code: `git clone git://github.com/defunkt/fakefs.git`
|
2009-10-07 05:00:06 +00:00
|
|
|
* Home: <http://github.com/defunkt/fakefs>
|
2009-10-07 04:07:28 +00:00
|
|
|
* Docs: <http://defunkt.github.com/fakefs>
|
|
|
|
* Bugs: <http://github.com/defunkt/fakefs/issues>
|
2009-10-07 04:12:14 +00:00
|
|
|
* List: <http://groups.google.com/group/fakefs>
|
2009-10-07 04:07:28 +00:00
|
|
|
* Test: <http://runcoderun.com/defunkt/fakefs>
|
2009-10-07 04:12:14 +00:00
|
|
|
* Gems: <http://gemcutter.org/gems/fakefs>
|
2009-10-07 04:07:28 +00:00
|
|
|
* Boss: Chris Wanstrath :: <http://github.com/defunkt>
|