Improve SpecHelpers to work consistently. Use 'include' instead of 'extend' + use_fakefs. Add specs.

This commit is contained in:
Scott Taylor 2009-10-30 00:26:02 -04:00
parent f659ddd193
commit 85d4fa1173
4 changed files with 78 additions and 11 deletions

View File

@ -1,37 +1,43 @@
# FakeFS::SpecHelpers provides a simple macro for RSpec example groups to turn FakeFS on and off. # 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 # To use it simply require '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 # example groups that you wish to use FakeFS in. For example:
# before and after hooks which will enable and disable FakeFS. For example:
# #
# require 'fakefs/safe'
# require 'fakefs/spec_helpers' # require 'fakefs/spec_helpers'
# #
# describe "Some specs that deal with files" do # describe "Some specs that deal with files" do
# extend FakeFS::SpecHelpers # include FakeFS::SpecHelpers
# use_fakefs
# ... # ...
# end # end
# #
# Alternatively, you can include FakeFS::SpecHelpers in all your example groups using RSpec's # Alternatively, you can include FakeFS::SpecHelpers in all your example groups using RSpec's
# configuration block in your spec helper: # configuration block in your spec helper:
# #
# require 'fakefs/safe'
# require 'fakefs/spec_helpers' # require 'fakefs/spec_helpers'
# #
# Spec::Runner.configure do |config| # Spec::Runner.configure do |config|
# config.extend FakeFS::SpecHelpers # config.include FakeFS::SpecHelpers
# end # end
# #
# If you do the above then use_fakefs will be available in all of your example groups. # If you do the above then use_fakefs will be available in all of your example groups.
# #
require 'fakefs/safe'
module FakeFS module FakeFS
module SpecHelpers module SpecHelpers
def use_fakefs def self.extended(example_group)
before(:each) do 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! FakeFS.activate!
end end
after(:each) do describe_block.after :each do
FakeFS.deactivate! FakeFS.deactivate!
FakeFS::FileSystem.clear FakeFS::FileSystem.clear
end end

View File

@ -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

1
spec/spec.opts Normal file
View File

@ -0,0 +1 @@
--color

3
spec/spec_helper.rb Normal file
View File

@ -0,0 +1,3 @@
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'fakefs/spec_helpers'