Skip to content
Browse files

Refactor mock adapter framework

- default is now rspec
  • Loading branch information...
1 parent b661059 commit 75b4d83af16732e7244cfa5f2124a30ba18f967e @dchelimsky dchelimsky committed
View
37 lib/rspec/core/configuration.rb
@@ -10,6 +10,8 @@ class Configuration
# Run all examples if the run is filtered, and no examples were found.
attr_writer :run_all_when_everything_filtered
+ attr_reader :options
+
def initialize
@run_all_when_everything_filtered = false
@hooks = {
@@ -48,31 +50,23 @@ def backtrace_clean_patterns
def mock_framework=(use_me_to_mock)
@options[:mock_framework] = use_me_to_mock
-
- mock_framework_class = case use_me_to_mock.to_s
+ end
+
+ def require_mock_framework_adapter
+ require case @options[:mock_framework].to_s
+ when ""
+ 'rspec/core/mocking/with_rspec'
when /rspec/i
- require 'rspec/core/mocking/with_rspec'
- Rspec::Core::Mocking::WithRspec
+ 'rspec/core/mocking/with_rspec'
when /mocha/i
- require 'rspec/core/mocking/with_mocha'
- Rspec::Core::Mocking::WithMocha
+ 'rspec/core/mocking/with_mocha'
when /rr/i
- require 'rspec/core/mocking/with_rr'
- Rspec::Core::Mocking::WithRR
+ 'rspec/core/mocking/with_rr'
when /flexmock/i
- require 'rspec/core/mocking/with_flexmock'
- Rspec::Core::Mocking::WithFlexmock
+ 'rspec/core/mocking/with_flexmock'
else
- require 'rspec/core/mocking/with_absolutely_nothing'
- Rspec::Core::Mocking::WithAbsolutelyNothing
+ 'rspec/core/mocking/with_absolutely_nothing'
end
-
- @options[:mock_framework_class] = mock_framework_class
- Rspec::Core::ExampleGroup.send(:include, mock_framework_class)
- end
-
- def mock_framework
- @options[:mock_framework]
end
def filename_pattern
@@ -224,6 +218,11 @@ def find_hook(hook, each_or_all, group)
end.map { |filters, block| block }
end
+ def configure_mock_framework
+ require_mock_framework_adapter
+ Rspec::Core::ExampleGroup.send(:include, Rspec::Core::MockFrameworkAdapter)
+ end
+
end
end
end
View
12 lib/rspec/core/mocking/with_absolutely_nothing.rb
@@ -1,13 +1,11 @@
module Rspec
module Core
- module Mocking
- module WithAbsolutelyNothing
+ module MockFrameworkAdapter
- def _setup_mocks; end
- def _verify_mocks; end
- def _teardown_mocks; end
+ def _setup_mocks; end
+ def _verify_mocks; end
+ def _teardown_mocks; end
- end
end
end
-end
+end
View
26 lib/rspec/core/mocking/with_flexmock.rb
@@ -7,19 +7,19 @@
module Rspec
module Core
- module Mocking
- module WithFlexmock
- include FlexMock::MockContainer
- def setup_mocks_for_rspec
- # No setup required
- end
- def verify_mocks_for_rspec
- flexmock_verify
- end
- def teardown_mocks_for_rspec
- flexmock_close
- end
+ module MockFrameworkAdapter
+
+ include FlexMock::MockContainer
+ def setup_mocks_for_rspec
+ # No setup required
+ end
+ def verify_mocks_for_rspec
+ flexmock_verify
end
+ def teardown_mocks_for_rspec
+ flexmock_close
+ end
+
end
end
-end
+end
View
25 lib/rspec/core/mocking/with_mocha.rb
@@ -3,20 +3,19 @@
module Rspec
module Core
- module Mocking
- module WithMocha
- # Mocha::Standalone was deprecated as of Mocha 0.9.7.
- begin
- include Mocha::API
- rescue NameError
- include Mocha::Standalone
- end
-
- alias :_setup_mocks :mocha_setup
- alias :_verify_mocks :mocha_verify
- alias :_teardown_mocks :mocha_teardown
+ module MockFrameworkAdapter
+ # Mocha::Standalone was deprecated as of Mocha 0.9.7.
+ begin
+ include Mocha::API
+ rescue NameError
+ include Mocha::Standalone
end
+
+ alias :_setup_mocks :mocha_setup
+ alias :_verify_mocks :mocha_verify
+ alias :_teardown_mocks :mocha_teardown
+
end
end
-end
+end
View
25 lib/rspec/core/mocking/with_rr.rb
@@ -4,23 +4,22 @@
module Rspec
module Core
- module Mocking
- module WithRR
- include RR::Extensions::InstanceMethods
+ module MockFrameworkAdapter
- def _setup_mocks
- RR::Space.instance.reset
- end
+ include RR::Extensions::InstanceMethods
- def _verify_mocks
- RR::Space.instance.verify_doubles
- end
+ def _setup_mocks
+ RR::Space.instance.reset
+ end
- def _teardown_mocks
- RR::Space.instance.reset
- end
+ def _verify_mocks
+ RR::Space.instance.verify_doubles
+ end
+ def _teardown_mocks
+ RR::Space.instance.reset
end
+
end
end
-end
+end
View
24 lib/rspec/core/mocking/with_rspec.rb
@@ -3,19 +3,19 @@
module Rspec
module Core
- module Mocking
- module WithRspec
- include Rspec::Mocks::ExampleMethods
- def _setup_mocks
- $rspec_mocks ||= Rspec::Mocks::Space.new
- end
- def _verify_mocks
- $rspec_mocks.verify_all
- end
- def _teardown_mocks
- $rspec_mocks.reset_all
- end
+ module MockFrameworkAdapter
+
+ include Rspec::Mocks::ExampleMethods
+ def _setup_mocks
+ $rspec_mocks ||= Rspec::Mocks::Space.new
+ end
+ def _verify_mocks
+ $rspec_mocks.verify_all
end
+ def _teardown_mocks
+ $rspec_mocks.reset_all
+ end
+
end
end
end
View
2 lib/rspec/core/runner.rb
@@ -29,6 +29,8 @@ def run(args = [])
Rspec::Core::CommandLineOptions.parse(args).apply(configuration)
require_all_files(configuration)
+
+ configuration.configure_mock_framework
total_examples_to_run = Rspec::Core.world.total_examples_to_run
View
34 spec/rspec/core/configuration_spec.rb
@@ -6,28 +6,34 @@ module Rspec::Core
let(:config) { Configuration.new }
- context "setting the mock framework" do
+ describe "#mock_framework_class" do
+ before(:each) do
+ config.stub(:require)
+ end
- it "requires and includes the rspec adapter when the mock_framework is :rspec" do
+ it "defaults to :rspec" do
config.should_receive(:require).with('rspec/core/mocking/with_rspec')
- ExampleGroup.should_receive(:send)
- config.mock_framework = :rspec
+ config.require_mock_framework_adapter
end
- it "supports mock_with for backward compatibility with rspec-1.x" do
- config.stub!(:require)
- ExampleGroup.stub!(:send)
- config.mock_with :rspec
+ [:rspec, :mocha, :rr, :flexmock].each do |framework|
+ it "uses #{framework.inspect} framework when set explicitly" do
+ config.should_receive(:require).with("rspec/core/mocking/with_#{framework}")
+ config.mock_framework = framework
+ config.require_mock_framework_adapter
+ end
end
-
- it "includes the null adapter when the mock_framework is not :rspec, :mocha, or :rr" do
- ExampleGroup.should_receive(:send).with(:include, Mocking::WithAbsolutelyNothing)
+
+ it "uses the null adapter when set to any unknown key" do
+ config.should_receive(:require).with('rspec/core/mocking/with_absolutely_nothing')
config.mock_framework = :crazy_new_mocking_framework_ive_not_yet_heard_of
+ config.require_mock_framework_adapter
end
- pending "includes the rspec adapter when the mock_framework is not set" do
- ExampleGroup.stub!(:send)
- config.mock_framework.should == :rspec
+ it "supports mock_with for backward compatibility with rspec-1.x" do
+ config.should_receive(:require).with('rspec/core/mocking/with_rspec')
+ config.mock_with :rspec
+ config.require_mock_framework_adapter
end
end
View
5 spec/spec_helper.rb
@@ -9,13 +9,11 @@
begin
require 'autotest'
rescue LoadError
- raise "You must install ZenTest to use autotest"
+ raise "You must install autotest to use it"
end
require 'autotest/rspec2'
-Rspec::Core::ExampleGroup.send(:include, Rspec::Matchers)
-
Dir['./spec/support/**/*.rb'].map {|f| require f}
module Rspec
@@ -45,7 +43,6 @@ def in_editor?
end
Rspec.configure do |c|
- c.mock_framework = :rspec
c.color_enabled = !in_editor?
c.exclusion_filter = { :ruby => lambda {|version|
!(RUBY_VERSION.to_s =~ /^#{version.to_s}/)

0 comments on commit 75b4d83

Please sign in to comment.
Something went wrong with that request. Please try again.