From b0830550fe0ebfc51d6c24dd2a4068b610850d55 Mon Sep 17 00:00:00 2001 From: Ben Mabey Date: Wed, 14 Oct 2009 22:27:37 +0800 Subject: [PATCH] rspec example group helpers to turn fakefs on/off --- README.markdown | 6 ++++++ lib/fakefs/spec_helpers.rb | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 lib/fakefs/spec_helpers.rb diff --git a/README.markdown b/README.markdown index ffd4246..98f73c3 100644 --- a/README.markdown +++ b/README.markdown @@ -47,6 +47,12 @@ Don't Fake the FS Immediately # your code end +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. + How is this different than MockFS? ---------------------------------- diff --git a/lib/fakefs/spec_helpers.rb b/lib/fakefs/spec_helpers.rb new file mode 100644 index 0000000..c9b45e5 --- /dev/null +++ b/lib/fakefs/spec_helpers.rb @@ -0,0 +1,38 @@ +# FakeFS::SpecHelpers provides a simple macro for RSpec example groups to turn FakeFS on and off. +# To use it simply require 'fakefs/safe' and 'fakefs/spec_helpers'. Then include FakeFS::SpecHelpers into any +# example groups that you wish to use FakeFS in. The "use_fakefs" macro is then available to install +# before and after hooks which will enable and disable FakeFS. For example: +# +# require 'fakefs/safe' +# require 'fakefs/spec_helpers' +# describe SomeClassThatDealsWithFiles +# include FakeFS::SpecHelpers +# use_fakefs +# ... +# end +# +# Alternatively, you can include FakeFS::SpecHelpers in all your example groups using RSpec's +# configuration block in your spec helper: +# +# require 'fakefs/safe' +# require 'fakefs/spec_helpers' +# Spec::Runner.configure do |config| +# config.extend FakeFS::SpecHelpers +# end +# +# If you do the above then use_fakefs will be available in all of your example groups. +# +module FakeFS + module SpecHelpers + def use_fakefs + before(:each) do + FakeFS.activate! + end + + after(:each) do + FakeFS.deactivate! + FakeFS::FileSystem.clear + end + end + end +end