Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Don't raise an error when the user is re-setting the existing config.

  • Loading branch information...
commit f2dc2f8954fc6663dc45f03375912dc58fc590f9 1 parent 511f4ba
@myronmarston myronmarston authored
View
31 lib/rspec/core/configuration.rb
@@ -147,10 +147,8 @@ def cleaned_from_backtrace?(line)
# Returns the configured mock framework adapter module
def mock_framework
- @mock_framework ||= begin
- require 'rspec/core/mocking/with_rspec'
- RSpec::Core::MockFrameworkAdapter
- end
+ mock_with :rspec unless @mock_framework
+ @mock_framework
end
# Delegates to mock_framework=(framework)
@@ -181,10 +179,9 @@ def mock_framework=(framework)
# teardown_mocks_for_rspec
# - called after verify_mocks_for_rspec (even if there are errors)
def mock_with(framework)
- assert_no_example_groups_defined(:mock_framework)
- case framework
+ framework_module = case framework
when Module
- @mock_framework = framework
+ framework
when String, Symbol
require case framework.to_s
when /rspec/i
@@ -198,8 +195,18 @@ def mock_with(framework)
else
'rspec/core/mocking/with_absolutely_nothing'
end
- @mock_framework = RSpec::Core::MockFrameworkAdapter
+ RSpec::Core::MockFrameworkAdapter
end
+
+ new_name, old_name = [framework_module, @mock_framework].map do |mod|
+ mod.respond_to?(:framework_name) ? mod.framework_name : :unnamed
+ end
+
+ unless new_name == old_name
+ assert_no_example_groups_defined(:mock_framework)
+ end
+
+ @mock_framework = framework_module
end
# Returns the configured expectation framework adapter module(s)
@@ -221,9 +228,6 @@ def expectation_framework=(framework)
# Given :stdlib, configures test/unit/assertions
# Given both, configures both
def expect_with(*frameworks)
- assert_no_example_groups_defined(:expect_with)
- @expectation_frameworks.clear
-
modules = frameworks.map do |framework|
case framework
when :rspec
@@ -238,6 +242,11 @@ def expect_with(*frameworks)
end
end
+ if (modules - @expectation_frameworks).any?
+ assert_no_example_groups_defined(:expect_with)
+ end
+
+ @expectation_frameworks.clear
@expectation_frameworks.push(*modules)
end
View
38 spec/rspec/core/configuration_spec.rb
@@ -83,12 +83,28 @@ module RSpec::Core
end
context 'when there are already some example groups defined' do
+ before(:each) { config.stub(:require) }
+
it 'raises an error since this setting must be applied before any groups are defined' do
RSpec.world.stub(:example_groups).and_return([double.as_null_object])
expect {
- config.mock_with :rspec
+ config.mock_with :mocha
}.to raise_error(/must be configured before any example groups are defined/)
end
+
+ it 'does not raise an error if the default `mock_with :rspec` is re-configured' do
+ config.mock_framework # called by RSpec when configuring the first example group
+ RSpec.world.stub(:example_groups).and_return([double.as_null_object])
+ config.mock_with :rspec
+ end
+
+ it 'does not raise an error if re-setting the same config' do
+ groups = []
+ RSpec.world.stub(:example_groups => groups)
+ config.mock_with :mocha
+ groups << double.as_null_object
+ config.mock_with :mocha
+ end
end
end
@@ -107,6 +123,12 @@ module RSpec::Core
end
describe "#expect_with" do
+ before(:each) do
+ # we need to prevent stdlib from being required because it defines a
+ # `pass` method that conflicts with our `pass` matcher.
+ config.stub(:require)
+ end
+
[
[:rspec, 'rspec/expectations'],
[:stdlib, 'test/unit/assertions']
@@ -132,6 +154,20 @@ module RSpec::Core
config.expect_with :rspec
}.to raise_error(/must be configured before any example groups are defined/)
end
+
+ it 'does not raise an error if the default `expect_with :rspec` is re-configured' do
+ config.expectation_frameworks # called by RSpec when configuring the first example group
+ RSpec.world.stub(:example_groups).and_return([double.as_null_object])
+ config.expect_with :rspec
+ end
+
+ it 'does not raise an error if re-setting the same config' do
+ groups = []
+ RSpec.world.stub(:example_groups => groups)
+ config.expect_with :stdlib
+ groups << double.as_null_object
+ config.expect_with :stdlib
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.