diff --git a/lib/fakefs/spec_helpers.rb b/lib/fakefs/spec_helpers.rb index c9b45e5..0123057 100644 --- a/lib/fakefs/spec_helpers.rb +++ b/lib/fakefs/spec_helpers.rb @@ -1,35 +1,43 @@ # 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: +# To use it simply require 'fakefs/spec_helpers', then include FakeFS::SpecHelpers into any +# example groups that you wish to use FakeFS in. For example: # -# require 'fakefs/safe' # require 'fakefs/spec_helpers' -# describe SomeClassThatDealsWithFiles +# +# describe "Some specs that deal with files" do # 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 +# config.include FakeFS::SpecHelpers # end # # If you do the above then use_fakefs will be available in all of your example groups. # +require 'fakefs/safe' + module FakeFS module SpecHelpers - def use_fakefs - before(:each) do + def self.extended(example_group) + example_group.use_fakefs(example_group) + end + + def self.included(example_group) + example_group.extend self + end + + def use_fakefs(describe_block) + describe_block.before :each do FakeFS.activate! end - after(:each) do + describe_block.after :each do FakeFS.deactivate! FakeFS::FileSystem.clear end diff --git a/spec/fakefs/spec_helpers_spec.rb b/spec/fakefs/spec_helpers_spec.rb new file mode 100644 index 0000000..8d73f03 --- /dev/null +++ b/spec/fakefs/spec_helpers_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper' + +module FakeFS + describe SpecHelpers do + before do + @rspec_example_group = Class.new do + def self.before(sym = :each) + yield if block_given? + end + + def self.after(sym = :each) + yield if block_given? + end + end + end + + describe "when extending" do + context "before each" do + it "should call it" do + @rspec_example_group.should_receive(:before).with(:each) + @rspec_example_group.extend FakeFS::SpecHelpers + end + + it "should call FakeFS.activate!" do + FakeFS.should_receive(:activate!) + @rspec_example_group.extend FakeFS::SpecHelpers + end + end + + context "after each" do + it "should call it" do + @rspec_example_group.should_receive(:after).with(:each) + @rspec_example_group.extend FakeFS::SpecHelpers + end + + it "should deactivate fakefs" do + FakeFS.should_receive(:deactivate!) + @rspec_example_group.extend FakeFS::SpecHelpers + end + + it "should clear the fakefs filesystem for the next run" do + FakeFS::FileSystem.should_receive(:clear) + @rspec_example_group.extend FakeFS::SpecHelpers + end + end + end + + describe "when including" do + it "should call before :each" do + @rspec_example_group.should_receive(:before) + @rspec_example_group.class_eval do + include FakeFS::SpecHelpers + end + end + end + end +end \ No newline at end of file diff --git a/spec/spec.opts b/spec/spec.opts new file mode 100644 index 0000000..5052887 --- /dev/null +++ b/spec/spec.opts @@ -0,0 +1 @@ +--color \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..6d83b80 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,3 @@ +$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') + +require 'fakefs/spec_helpers' \ No newline at end of file