diff --git a/Changelog.md b/Changelog.md index 7aaf624f43..e403599e4c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -3,6 +3,9 @@ Breaking Changes: * Ruby < 2.3 is no longer supported. (Phil Pirozhkov, #2787) +* Extract `should` syntax (including the non-monkey-patching one liner). (Phil Pirozhkov, #2803) +* Remove globally-exposed DSL (example and shared group methods + in the root scope and on Module). (Phil Pirozhkov, #2803) Enhancements: diff --git a/features/configuration/enable_global_dsl.feature b/features/configuration/enable_global_dsl.feature deleted file mode 100644 index cf0d38f0ea..0000000000 --- a/features/configuration/enable_global_dsl.feature +++ /dev/null @@ -1,74 +0,0 @@ -Feature: Global namespace DSL - - RSpec has a few top-level constructs that allow you to begin describing - behaviour: - - * `RSpec.describe`: Define a named context for a group of examples. - - * `RSpec.shared_examples`: Define a set of shared examples that can later be - included in an example group. - - * `RSpec.shared_context`: define some common context (using `before`, `let`, - helper methods, etc) that can later be included in an example group. - - Historically, these constructs have been available directly off of the main - object, so that you could use these at the start of a file without the - `RSpec.` prefix. They have also been available off of any class or module so - that you can scope your examples within a particular constant namespace. - - RSpec 3 now provides an option to disable this global monkey patching: - - ```ruby - config.expose_dsl_globally = false - ``` - - For backwards compatibility it defaults to `true`. - - @allow-should-syntax - Scenario: By default RSpec allows the DSL to be used globally - Given a file named "spec/example_spec.rb" with: - """ruby - describe "specs here" do - it "passes" do - end - end - """ - When I run `rspec` - Then the output should contain "1 example, 0 failures" - - @allow-should-syntax - Scenario: By default rspec/autorun allows the DSL to be used globally - Given a file named "spec/example_spec.rb" with: - """ruby - require 'rspec/autorun' - describe "specs here" do - it "passes" do - end - end - """ - When I run `ruby spec/example_spec.rb` - Then the output should contain "1 example, 0 failures" - - Scenario: When exposing globally is disabled the top level DSL no longer works - Given a file named "spec/example_spec.rb" with: - """ruby - RSpec.configure { |c| c.expose_dsl_globally = false } - describe "specs here" do - it "passes" do - end - end - """ - When I run `rspec` - Then the output should contain "undefined method `describe'" - - Scenario: Regardless of setting - Given a file named "spec/example_spec.rb" with: - """ruby - RSpec.configure { |c| c.expose_dsl_globally = true } - RSpec.describe "specs here" do - it "passes" do - end - end - """ - When I run `rspec` - Then the output should contain "1 example, 0 failures" diff --git a/features/configuration/zero_monkey_patching_mode.feature b/features/configuration/zero_monkey_patching_mode.feature deleted file mode 100644 index f38356b22e..0000000000 --- a/features/configuration/zero_monkey_patching_mode.feature +++ /dev/null @@ -1,106 +0,0 @@ -@allow-should-syntax -Feature: Zero monkey patching mode - - Use the `disable_monkey_patching!` configuration option to - disable all monkey patching done by RSpec: - - - stops exposing DSL globally - - disables `should` and `should_not` syntax for rspec-expectations - - disables `stub`, `should_receive`, and `should_not_receive` syntax for - rspec-mocks - - ```ruby - RSpec.configure { |c| c.disable_monkey_patching! } - ``` - - Background: - Given a file named "spec/example_describe_spec.rb" with: - """ruby - require 'spec_helper' - - describe "specs here" do - it "passes" do - end - end - """ - Given a file named "spec/example_should_spec.rb" with: - """ruby - require 'spec_helper' - - RSpec.describe "another specs here" do - it "passes with monkey patched expectations" do - x = 25 - x.should eq 25 - x.should_not be > 30 - end - - it "passes with monkey patched mocks" do - x = double("thing") - x.stub(:hello => [:world]) - x.should_receive(:count).and_return(4) - x.should_not_receive(:all) - (x.hello * x.count).should eq([:world, :world, :world, :world]) - end - end - """ - Given a file named "spec/example_expect_spec.rb" with: - """ruby - require 'spec_helper' - - RSpec.describe "specs here too" do - it "passes in zero monkey patching mode" do - x = double("thing") - allow(x).to receive(:hello).and_return([:world]) - expect(x).to receive(:count).and_return(4) - expect(x).not_to receive(:all) - expect(x.hello * x.count).to eq([:world, :world, :world, :world]) - end - - it "passes in zero monkey patching mode" do - x = 25 - expect(x).to eq(25) - expect(x).not_to be > 30 - end - end - """ - - Scenario: By default RSpec allows monkey patching - Given a file named "spec/spec_helper.rb" with: - """ruby - # Empty spec_helper - """ - When I run `rspec` - Then the examples should all pass - - Scenario: Monkey patched methods are undefined with `disable_monkey_patching!` - Given a file named "spec/spec_helper.rb" with: - """ruby - RSpec.configure do |config| - config.disable_monkey_patching! - end - """ - When I run `rspec spec/example_should_spec.rb` - Then the output should contain all of these: - | undefined method `should' | - | unexpected message :stub | - When I run `rspec spec/example_describe_spec.rb` - Then the output should contain "undefined method `describe'" - - Scenario: `allow` and `expect` syntax works with monkey patching - Given a file named "spec/spec_helper.rb" with: - """ruby - # Empty spec_helper - """ - When I run `rspec spec/example_expect_spec.rb` - Then the examples should all pass - - Scenario: `allow` and `expect` syntax works without monkey patching - Given a file named "spec/spec_helper.rb" with: - """ruby - RSpec.configure do |config| - config.disable_monkey_patching! - end - """ - When I run `rspec spec/example_expect_spec.rb` - Then the examples should all pass - diff --git a/features/subject/one_liner_syntax.feature b/features/subject/one_liner_syntax.feature index 70a2df876b..fdcedfc236 100644 --- a/features/subject/one_liner_syntax.feature +++ b/features/subject/one_liner_syntax.feature @@ -1,22 +1,13 @@ -@oneliner-should Feature: One-liner syntax - RSpec supports a one-liner syntax for setting an expectation on the - `subject`. RSpec will give the examples a doc string that is auto- + RSpec supports a one-liner syntax, `is_expected`, for setting an expectation + on the `subject`. RSpec will give the examples a doc string that is auto- generated from the matcher used in the example. This is designed specifically to help avoid duplication in situations where the doc string and the matcher used in the example mirror each other exactly. When used excessively, it can produce documentation output that does not read well or contribute to - understanding the object you are describing. - - This comes in two flavors: - - * `is_expected` is defined simply as `expect(subject)` and is designed for - when you are using rspec-expectations with its newer expect-based syntax. - * `should` was designed back when rspec-expectations only had a should-based - syntax. However, it continues to be available and work even if the - `:should` syntax is disabled (since that merely removes `Object#should` - but this is `RSpec::Core::ExampleGroup#should`). + understanding the object you are describing. This syntax is a shorthand for + `expect(subject)`. Notes: @@ -29,13 +20,6 @@ Feature: One-liner syntax """ruby RSpec.describe Array do describe "when first created" do - # Rather than: - # it "should be empty" do - # subject.should be_empty - # end - - it { should be_empty } - # or it { is_expected.to be_empty } end end @@ -47,7 +31,6 @@ Feature: One-liner syntax Array when first created is expected to be empty - is expected to be empty """ Scenario: Explicit subject @@ -56,8 +39,6 @@ Feature: One-liner syntax RSpec.describe Array do describe "with 3 items" do subject { [1,2,3] } - it { should_not be_empty } - # or it { is_expected.not_to be_empty } end end @@ -69,5 +50,4 @@ Feature: One-liner syntax Array with 3 items is expected not to be empty - is expected not to be empty """ diff --git a/features/support/require_expect_syntax_in_aruba_specs.rb b/features/support/require_expect_syntax_in_aruba_specs.rb index 5bd2519093..733e707abd 100644 --- a/features/support/require_expect_syntax_in_aruba_specs.rb +++ b/features/support/require_expect_syntax_in_aruba_specs.rb @@ -1,29 +1,10 @@ if defined?(Cucumber) require 'shellwords' - Before('~@allow-should-syntax', '~@with-clean-spec-opts') do + Before('~@with-clean-spec-opts') do set_environment_variable('SPEC_OPTS', "-r#{Shellwords.escape(__FILE__)}") end - - Before('@oneliner-should') do - set_environment_variable('ALLOW_ONELINER_SHOULD', 'true') - end else if ENV['REMOVE_OTHER_RSPEC_LIBS_FROM_LOAD_PATH'] $LOAD_PATH.reject! { |x| /rspec-mocks/ === x || /rspec-expectations/ === x } end - - module DisallowOneLinerShould - def should(*) - raise "one-liner should is not allowed" - end - - def should_not(*) - raise "one-liner should_not is not allowed" - end - end - - RSpec.configure do |rspec| - rspec.disable_monkey_patching! - rspec.include DisallowOneLinerShould unless ENV['ALLOW_ONELINER_SHOULD'] - end end diff --git a/lib/rspec/core.rb b/lib/rspec/core.rb index bd9a51fd7f..1d4c0999bd 100644 --- a/lib/rspec/core.rb +++ b/lib/rspec/core.rb @@ -178,7 +178,4 @@ def self.const_missing(name) require MODULES_TO_AUTOLOAD.fetch(name) { return super } ::RSpec.const_get(name) end - - Core::DSL.expose_globally! - Core::SharedExampleGroup::TopLevelDSL.expose_globally! end diff --git a/lib/rspec/core/configuration.rb b/lib/rspec/core/configuration.rb index 8c673b5200..31daea7789 100644 --- a/lib/rspec/core/configuration.rb +++ b/lib/rspec/core/configuration.rb @@ -138,27 +138,6 @@ def default_path=(path) # Default: `$stderr`. add_setting :error_stream - # Indicates if the DSL has been exposed off of modules and `main`. - # Default: true - # @return [Boolean] - def expose_dsl_globally? - Core::DSL.exposed_globally? - end - - # Use this to expose the core RSpec DSL via `Module` and the `main` - # object. It will be set automatically but you can override it to - # remove the DSL. - # Default: true - def expose_dsl_globally=(value) - if value - Core::DSL.expose_globally! - Core::SharedExampleGroup::TopLevelDSL.expose_globally! - else - Core::DSL.remove_globally! - Core::SharedExampleGroup::TopLevelDSL.remove_globally! - end - end - # Determines where deprecation warnings are printed. # Defaults to `$stderr`. # @return [IO, String] IO or filename to write to @@ -1179,11 +1158,7 @@ def alias_example_to(name, *args) # RSpec.describe User, :type => :model do # end # - # @note The defined aliased will also be added to the top level - # (e.g. `main` and from within modules) if - # `expose_dsl_globally` is set to true. # @see #alias_example_to - # @see #expose_dsl_globally= def alias_example_group_to(new_name, *args) extra_options = Metadata.build_hash_from(args) RSpec::Core::ExampleGroup.define_example_group_method(new_name, extra_options) @@ -1586,7 +1561,6 @@ def in_project_source_dir_regex # @private def configure_mock_framework RSpec::Core::ExampleGroup.include(mock_framework) - conditionally_disable_mocks_monkey_patching end # @private @@ -1594,7 +1568,6 @@ def configure_expectation_framework expectation_frameworks.each do |framework| RSpec::Core::ExampleGroup.include(framework) end - conditionally_disable_expectations_monkey_patching end # @private @@ -1804,52 +1777,6 @@ def raise_errors_for_deprecations! self.deprecation_stream = Formatters::DeprecationFormatter::RaiseErrorStream.new end - # Enables zero monkey patching mode for RSpec. It removes monkey - # patching of the top-level DSL methods (`describe`, - # `shared_examples_for`, etc) onto `main` and `Module`, instead - # requiring you to prefix these methods with `RSpec.`. It enables - # expect-only syntax for rspec-mocks and rspec-expectations. It - # simply disables monkey patching on whatever pieces of RSpec - # the user is using. - # - # @note It configures rspec-mocks and rspec-expectations only - # if the user is using those (either explicitly or implicitly - # by not setting `mock_with` or `expect_with` to anything else). - # - # @note If the user uses this options with `mock_with :mocha` - # (or similiar) they will still have monkey patching active - # in their test environment from mocha. - # - # @example - # - # # It disables all monkey patching. - # RSpec.configure do |config| - # config.disable_monkey_patching! - # end - # - # # Is an equivalent to - # RSpec.configure do |config| - # config.expose_dsl_globally = false - # - # config.mock_with :rspec do |mocks| - # mocks.syntax = :expect - # mocks.patch_marshal_to_support_partial_doubles = false - # end - # - # config.expect_with :rspec do |expectations| - # expectations.syntax = :expect - # end - # end - def disable_monkey_patching! - self.expose_dsl_globally = false - self.disable_monkey_patching = true - conditionally_disable_mocks_monkey_patching - conditionally_disable_expectations_monkey_patching - end - - # @private - attr_accessor :disable_monkey_patching - # Defines a callback that can assign derived metadata values. # # @param filters [Array, Hash] metadata filters that determine @@ -2260,22 +2187,6 @@ def output_to_tty?(output=output_stream) output.respond_to?(:tty?) && output.tty? end - def conditionally_disable_mocks_monkey_patching - return unless disable_monkey_patching && rspec_mocks_loaded? - - RSpec::Mocks.configuration.tap do |config| - config.syntax = :expect if config.respond_to?(:syntax) - config.patch_marshal_to_support_partial_doubles = false - end - end - - def conditionally_disable_expectations_monkey_patching - return unless disable_monkey_patching && rspec_expectations_loaded? - return unless RSpec::Expectations.configuration.respond_to?(:syntax=) - - RSpec::Expectations.configuration.syntax = :expect - end - def rspec_mocks_loaded? defined?(RSpec::Mocks.configuration) end diff --git a/lib/rspec/core/dsl.rb b/lib/rspec/core/dsl.rb index 220403e6e3..b7015bec6e 100644 --- a/lib/rspec/core/dsl.rb +++ b/lib/rspec/core/dsl.rb @@ -1,9 +1,7 @@ module RSpec module Core # DSL defines methods to group examples, most notably `describe`, - # and exposes them as class methods of {RSpec}. They can also be - # exposed globally (on `main` and instances of `Module`) through - # the {Configuration} option `expose_dsl_globally`. + # and exposes them as class methods of {RSpec}. # # By default the methods `describe`, `context` and `example_group` # are exposed. These methods define a named context for one or @@ -28,11 +26,6 @@ def self.example_group_aliases @example_group_aliases ||= [] end - # @private - def self.exposed_globally? - @exposed_globally ||= false - end - # @private def self.expose_example_group_alias(name) return if example_group_aliases.include?(name) @@ -44,52 +37,12 @@ def self.expose_example_group_alias(name) RSpec.world.record(group) group end - - expose_example_group_alias_globally(name) if exposed_globally? end class << self # @private attr_accessor :top_level end - - # Adds the describe method to Module and the top level binding. - # @api private - def self.expose_globally! - return if exposed_globally? - - example_group_aliases.each do |method_name| - expose_example_group_alias_globally(method_name) - end - - @exposed_globally = true - end - - # Removes the describe method from Module and the top level binding. - # @api private - def self.remove_globally! - return unless exposed_globally? - - example_group_aliases.each do |method_name| - change_global_dsl { undef_method method_name } - end - - @exposed_globally = false - end - - # @private - def self.expose_example_group_alias_globally(method_name) - change_global_dsl do - remove_method(method_name) if method_defined?(method_name) - define_method(method_name) { |*a, &b| ::RSpec.__send__(method_name, *a, &b) } - end - end - - # @private - def self.change_global_dsl(&changes) - (class << top_level; self; end).class_exec(&changes) - Module.class_exec(&changes) - end end end end diff --git a/lib/rspec/core/example.rb b/lib/rspec/core/example.rb index d335801f7c..5b6464a75d 100644 --- a/lib/rspec/core/example.rb +++ b/lib/rspec/core/example.rb @@ -36,7 +36,7 @@ module Core # shared_examples "auditable" do # it "does something" do # log "#{example.full_description}: #{auditable.inspect}" - # auditable.should do_something + # expect(auditable).to do_something # end # end # diff --git a/lib/rspec/core/memoized_helpers.rb b/lib/rspec/core/memoized_helpers.rb index b0eb210561..6c2389457b 100644 --- a/lib/rspec/core/memoized_helpers.rb +++ b/lib/rspec/core/memoized_helpers.rb @@ -12,8 +12,6 @@ module MemoizedHelpers # # RSpec.describe Widget do # it { is_expected.to validate_presence_of(:name) } - # # or - # it { should validate_presence_of(:name) } # end # # While the examples below demonstrate how to use `subject` @@ -25,25 +23,21 @@ module MemoizedHelpers # # Explicit declaration of subject. # RSpec.describe Person do # subject { Person.new(:birthdate => 19.years.ago) } - # it "should be eligible to vote" do - # subject.should be_eligible_to_vote - # # ^ ^ explicit reference to subject not recommended + # it "is eligible to vote" do + # is_expected.to be_eligible_to_vote # end # end # # # Implicit subject => { Person.new }. # RSpec.describe Person do - # it "should be eligible to vote" do - # subject.should be_eligible_to_vote - # # ^ ^ explicit reference to subject not recommended + # it "is eligible to vote" do + # is_expected.to be_eligible_to_vote # end # end # # # One-liner syntax - expectation is set on the subject. # RSpec.describe Person do # it { is_expected.to be_eligible_to_vote } - # # or - # it { should be_eligible_to_vote } # end # # @note Because `subject` is designed to create state that is reset @@ -51,8 +45,6 @@ module MemoizedHelpers # state that is shared across _all_ examples in an example group, # `subject` is _not_ intended to be used in a `before(:context)` hook. # - # @see #should - # @see #should_not # @see #is_expected def subject __memoized.fetch_or_store(:subject) do @@ -61,45 +53,6 @@ def subject end end - # When `should` is called with no explicit receiver, the call is - # delegated to the object returned by `subject`. Combined with an - # implicit subject this supports very concise expressions. - # - # @example - # - # RSpec.describe Person do - # it { should be_eligible_to_vote } - # end - # - # @see #subject - # @see #is_expected - # - # @note This only works if you are using rspec-expectations. - # @note If you are using RSpec's newer expect-based syntax you may - # want to use `is_expected.to` instead of `should`. - def should(matcher=nil, message=nil) - RSpec::Expectations::PositiveExpectationHandler.handle_matcher(subject, matcher, message) - end - - # Just like `should`, `should_not` delegates to the subject (implicit or - # explicit) of the example group. - # - # @example - # - # RSpec.describe Person do - # it { should_not be_eligible_to_vote } - # end - # - # @see #subject - # @see #is_expected - # - # @note This only works if you are using rspec-expectations. - # @note If you are using RSpec's newer expect-based syntax you may - # want to use `is_expected.to_not` instead of `should_not`. - def should_not(matcher=nil, message=nil) - RSpec::Expectations::NegativeExpectationHandler.handle_matcher(subject, matcher, message) - end - # Wraps the `subject` in `expect` to make it the target of an expectation. # Designed to read nicely for one-liners. # @@ -111,8 +64,6 @@ def should_not(matcher=nil, message=nil) # end # # @see #subject - # @see #should - # @see #should_not # # @note This only works if you are using rspec-expectations. def is_expected @@ -278,7 +229,7 @@ module ClassMethods # thing.do_something # # # Second invocation, returns the memoized value. - # thing.should be_something + # expect(thing).to be_something # end # end def let(name, &block) @@ -349,12 +300,12 @@ def let(name, &block) # let(:thing) { Thing.new } # # it "is not invoked implicitly" do - # Thing.count.should eq(0) + # expect(Thing.count).to eq(0) # end # # it "can be invoked explicitly" do # thing - # Thing.count.should eq(1) + # expect(Thing.count).to eq(1) # end # end # @@ -362,12 +313,12 @@ def let(name, &block) # let!(:thing) { Thing.new } # # it "is invoked implicitly" do - # Thing.count.should eq(1) + # expect(Thing.count).to eq(1) # end # # it "returns memoized version on first invocation" do # thing - # Thing.count.should eq(1) + # expect(Thing.count).to eq(1) # end # end # end @@ -408,12 +359,10 @@ def let!(name, &block) # subject(:account) { CheckingAccount.new(Money.new(50, :USD)) } # it { is_expected.not_to be_overdrawn } # it "has a balance equal to the starting balance" do - # account.balance.should eq(Money.new(50, :USD)) + # expect(account.balance).to eq(Money.new(50, :USD)) # end # end # - # @see MemoizedHelpers#should - # @see MemoizedHelpers#should_not # @see MemoizedHelpers#is_expected def subject(name=nil, &block) if name @@ -459,12 +408,12 @@ def subject(name=nil, &block) # subject { Thing.new } # # it "is not invoked implicitly" do - # Thing.count.should eq(0) + # expect(Thing.count).to eq(0) # end # # it "can be invoked explicitly" do # subject - # Thing.count.should eq(1) + # expect(Thing.count).to eq(1) # end # end # @@ -472,12 +421,12 @@ def subject(name=nil, &block) # subject!(:thing) { Thing.new } # # it "is invoked implicitly" do - # Thing.count.should eq(1) + # expect(Thing.count).to eq(1) # end # # it "returns memoized version on first invocation" do # subject - # Thing.count.should eq(1) + # expect(Thing.count).to eq(1) # end # end # end diff --git a/lib/rspec/core/project_initializer/spec/spec_helper.rb b/lib/rspec/core/project_initializer/spec/spec_helper.rb index dcb9fa8770..63bdc7686f 100644 --- a/lib/rspec/core/project_initializer/spec/spec_helper.rb +++ b/lib/rspec/core/project_initializer/spec/spec_helper.rb @@ -59,13 +59,6 @@ # you configure your source control system to ignore this file. config.example_status_persistence_file_path = "spec/examples.txt" - # Limits the available syntax to the non-monkey patched syntax that is - # recommended. For more details, see: - # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ - # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ - # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode - config.disable_monkey_patching! - # This setting enables warnings. It's recommended, but in some cases may # be too noisy due to issues in dependencies. config.warnings = true diff --git a/lib/rspec/core/shared_example_group.rb b/lib/rspec/core/shared_example_group.rb index 247e42ced3..1bccff73be 100644 --- a/lib/rspec/core/shared_example_group.rb +++ b/lib/rspec/core/shared_example_group.rb @@ -114,35 +114,6 @@ def shared_examples(name, *args, &block) alias shared_examples_for shared_examples end end - - # @private - def self.exposed_globally? - @exposed_globally ||= false - end - - # @api private - # - # Adds the top level DSL methods to Module and the top level binding. - def self.expose_globally! - return if exposed_globally? - Core::DSL.change_global_dsl(&definitions) - @exposed_globally = true - end - - # @api private - # - # Removes the top level DSL methods to Module and the top level binding. - def self.remove_globally! - return unless exposed_globally? - - Core::DSL.change_global_dsl do - undef shared_examples - undef shared_context - undef shared_examples_for - end - - @exposed_globally = false - end end # @private diff --git a/spec/rspec/core/configuration_spec.rb b/spec/rspec/core/configuration_spec.rb index 4f3a95a152..9bf807e1d7 100644 --- a/spec/rspec/core/configuration_spec.rb +++ b/spec/rspec/core/configuration_spec.rb @@ -312,13 +312,10 @@ def mod.configuration; @config ||= Struct.new(:custom_setting).new; end it_behaves_like "a configurable framework adapter", :mock_with it "allows rspec-mocks to be configured with a provided block" do - skip unless RSpec::Mocks.configuration.respond_to?(:add_stub_and_should_receive_to) - mod = Module.new - - expect(RSpec::Mocks.configuration).to receive(:add_stub_and_should_receive_to).with(mod) + expect(RSpec::Mocks.configuration).to receive(:verify_partial_doubles=).with(true) config.mock_with :rspec do |c| - c.add_stub_and_should_receive_to mod + c.verify_partial_doubles = true end end @@ -2376,20 +2373,13 @@ def metadata_hash(*args) end it 'overrides existing definitions of the aliased method name without issueing warnings' do - config.expose_dsl_globally = true - class << ExampleGroup def my_group_method; :original; end end - Module.class_exec do - def my_group_method; :original; end - end - config.alias_example_group_to :my_group_method expect(ExampleGroup.my_group_method).to be < ExampleGroup - expect(Module.new.my_group_method).to be < ExampleGroup end it "allows adding additional metadata" do @@ -2717,114 +2707,6 @@ def strategy.order(list) end end - describe '#disable_monkey_patching!' do - let!(:config) { RSpec.configuration } - let!(:expectations) { RSpec::Expectations } - let!(:mocks) { RSpec::Mocks } - - def in_fully_monkey_patched_rspec_environment - in_sub_process do - config.expose_dsl_globally = true - if mocks.configuration.respond_to?(:syntax) - mocks.configuration.syntax = [:expect, :should] - end - mocks.configuration.patch_marshal_to_support_partial_doubles = true - if expectations.configuration.respond_to?(:syntax) - expectations.configuration.syntax = [:expect, :should] - end - - yield - end - end - - it 'stops exposing the DSL methods globally' do - in_fully_monkey_patched_rspec_environment do - mod = Module.new - expect { - config.disable_monkey_patching! - }.to change { mod.respond_to?(:describe) }.from(true).to(false) - end - end - - it 'stops using should syntax for expectations' do - skip unless expectations.configuration.respond_to?(:syntax) - in_fully_monkey_patched_rspec_environment do - obj = Object.new - config.expect_with :rspec - expect { - config.disable_monkey_patching! - }.to change { obj.respond_to?(:should) }.from(true).to(false) - end - end - - it 'stops using should syntax for mocks' do - skip unless mocks.configuration.respond_to?(:syntax) - in_fully_monkey_patched_rspec_environment do - obj = Object.new - config.mock_with :rspec - expect { - config.disable_monkey_patching! - }.to change { obj.respond_to?(:should_receive) }.from(true).to(false) - end - end - - it 'stops patching of Marshal' do - in_fully_monkey_patched_rspec_environment do - expect { - config.disable_monkey_patching! - }.to change { Marshal.respond_to?(:dump_with_rspec_mocks) }.from(true).to(false) - end - end - - context 'when user did not configure mock framework' do - def emulate_not_configured_mock_framework - in_fully_monkey_patched_rspec_environment do - allow(config).to receive(:rspec_mocks_loaded?).and_return(false, true) - config.instance_variable_set :@mock_framework, nil - ExampleGroup.send :remove_class_variable, :@@example_groups_configured - - yield - end - end - - it 'disables monkey patching after example groups being configured' do - skip unless mocks.configuration.respond_to?(:syntax) - emulate_not_configured_mock_framework do - obj = Object.new - config.disable_monkey_patching! - - expect { - ExampleGroup.ensure_example_groups_are_configured - }.to change { obj.respond_to?(:should_receive) }.from(true).to(false) - end - end - end - - context 'when user did not configure expectation framework' do - def emulate_not_configured_expectation_framework - in_fully_monkey_patched_rspec_environment do - allow(config).to receive(:rspec_expectations_loaded?).and_return(false, true) - config.instance_variable_set :@expectation_frameworks, [] - ExampleGroup.send :remove_class_variable, :@@example_groups_configured - - yield - end - end - - it 'disables monkey patching after example groups being configured' do - skip unless expectations.configuration.respond_to?(:syntax) - emulate_not_configured_expectation_framework do - obj = Object.new - config.disable_monkey_patching! - - expect { - ExampleGroup.ensure_example_groups_are_configured - }.to change { obj.respond_to?(:should) }.from(true).to(false) - end - end - end - end - describe 'recording spec start time (for measuring load)' do it 'returns a time' do expect(config.start_time).to be_an_instance_of ::Time diff --git a/spec/rspec/core/dsl_spec.rb b/spec/rspec/core/dsl_spec.rb index cb204f7a8a..16661c3239 100644 --- a/spec/rspec/core/dsl_spec.rb +++ b/spec/rspec/core/dsl_spec.rb @@ -6,57 +6,22 @@ include RSpec::Support::InSubProcess shared_examples_for "dsl methods" do |*method_names| - context "when expose_dsl_globally is enabled" do - def enable - in_sub_process do - changing_expose_dsl_globally do - RSpec.configuration.expose_dsl_globally = true - expect(RSpec.configuration.expose_dsl_globally?).to eq true - end - - yield - end - end - - it 'makes them only available off of `RSpec`, `main` and modules' do - enable do - expect(::RSpec).to respond_to(*method_names) - expect(main).to respond_to(*method_names) - expect(Module.new).to respond_to(*method_names) - - expect(Object.new).not_to respond_to(*method_names) - end - end - end - - context "when expose_dsl_globally is disabled" do - def disable - in_sub_process do - changing_expose_dsl_globally do - RSpec.configuration.expose_dsl_globally = false - expect(RSpec.configuration.expose_dsl_globally?).to eq false - end - - yield - end - end + it 'are only available off of `RSpec`' do + in_sub_process do + setup - it 'makes them only available off of `RSpec`' do - disable do - expect(::RSpec).to respond_to(*method_names) + expect(::RSpec).to respond_to(*method_names) - expect(main).not_to respond_to(*method_names) - expect(Module.new).not_to respond_to(*method_names) - expect(Object.new).not_to respond_to(*method_names) - end + expect(main).not_to respond_to(*method_names) + expect(Module.new).not_to respond_to(*method_names) + expect(Object.new).not_to respond_to(*method_names) end end end describe "built in DSL methods" do include_examples "dsl methods", :describe, :context, :shared_examples, :shared_examples_for, :shared_context do - def changing_expose_dsl_globally - yield + def setup end end end @@ -64,17 +29,7 @@ def changing_expose_dsl_globally describe "custom example group aliases" do context "when adding aliases before exposing the DSL globally" do include_examples "dsl methods", :detail do - def changing_expose_dsl_globally - RSpec.configuration.alias_example_group_to(:detail) - yield - end - end - end - - context "when adding aliases after exposing the DSL globally" do - include_examples "dsl methods", :detail do - def changing_expose_dsl_globally - yield + def setup RSpec.configuration.alias_example_group_to(:detail) end end @@ -88,18 +43,6 @@ def changing_expose_dsl_globally expect(RSpec::Core::DSL.example_group_aliases.count(:detail)).to eq(1) end end - - it "does not undefine the alias multiple times", :issue => 1824 do - in_sub_process do - RSpec.configuration.expose_dsl_globally = true - RSpec.configuration.alias_example_group_to(:detail) - RSpec.configuration.alias_example_group_to(:detail) - - expect { - RSpec.configuration.expose_dsl_globally = false - }.not_to raise_error - end - end end end end diff --git a/spec/rspec/core/example_group_spec.rb b/spec/rspec/core/example_group_spec.rb index 4146329a2c..b07a5e4334 100644 --- a/spec/rspec/core/example_group_spec.rb +++ b/spec/rspec/core/example_group_spec.rb @@ -1873,7 +1873,7 @@ def foo; end # for users. RSpec internals should not add methods here, though. expect(rspec_core_methods.map(&:to_sym)).to contain_exactly( :described_class, :subject, - :is_expected, :should, :should_not, + :is_expected, :pending, :skip, :setup_mocks_for_rspec, :teardown_mocks_for_rspec, :verify_mocks_for_rspec ) diff --git a/spec/rspec/core/memoized_helpers_spec.rb b/spec/rspec/core/memoized_helpers_spec.rb index dc2da63f82..7670755db9 100644 --- a/spec/rspec/core/memoized_helpers_spec.rb +++ b/spec/rspec/core/memoized_helpers_spec.rb @@ -107,7 +107,7 @@ def working_with?(double) end.new 1 end - it { should be_working_with double(:value => 10) } + it { is_expected.to be_working_with double(:value => 10) } end [false, nil].each do |falsy_value| @@ -331,9 +331,9 @@ def should_raise_not_supported_error(&block) def ok?; true; end def not_ok?; false; end - it { should eq(self) } - it { should be_ok } - it { should_not be_not_ok } + it { is_expected.to eq(self) } + it { is_expected.to be_ok } + it { is_expected.not_to be_not_ok } end expect(group.run).to be true diff --git a/spec/rspec/core/metadata_spec.rb b/spec/rspec/core/metadata_spec.rb index 3c59837adf..9cebe65ce2 100644 --- a/spec/rspec/core/metadata_spec.rb +++ b/spec/rspec/core/metadata_spec.rb @@ -26,7 +26,7 @@ module Core expect([nil, "."]).to include(value) end - it 'should not transform directories beginning with the same prefix' do + it 'does not transform directories beginning with the same prefix' do #E.g. /foo/bar_baz is not relative to /foo/bar !! similar_directory = "#{File.expand_path(".")}_similar" diff --git a/spec/rspec/core/runner_spec.rb b/spec/rspec/core/runner_spec.rb index 83db213c7e..8c621a5a68 100644 --- a/spec/rspec/core/runner_spec.rb +++ b/spec/rspec/core/runner_spec.rb @@ -177,7 +177,7 @@ def interrupt instance_double(::DRb::DRbServer, :uri => "druby://127.0.0.1:0000/", :alive? => true) end - it { should be_truthy } + it { is_expected.to be_truthy } end context "when drb server is started with localhost" do @@ -185,7 +185,7 @@ def interrupt instance_double(::DRb::DRbServer, :uri => "druby://localhost:0000/", :alive? => true) end - it { should be_truthy } + it { is_expected.to be_truthy } end context "when drb server is started with another local ip address" do @@ -197,7 +197,7 @@ def interrupt allow(::IPSocket).to receive(:getaddress).and_return("192.168.0.1") end - it { should be_truthy } + it { is_expected.to be_truthy } end context "when drb server is started with 127.0.0.1 but not alive" do @@ -205,7 +205,7 @@ def interrupt instance_double(::DRb::DRbServer, :uri => "druby://127.0.0.1:0000/", :alive? => false) end - it { should be_falsey } + it { is_expected.to be_falsey } end context "when IPSocket cannot resolve the current hostname" do @@ -219,7 +219,7 @@ def interrupt ) end - it { should be_falsey } + it { is_expected.to be_falsey } end context "when no drb server is running" do @@ -227,7 +227,7 @@ def interrupt raise ::DRb::DRbServerNotFound end - it { should be_falsey } + it { is_expected.to be_falsey } end end diff --git a/spec/rspec/core/shared_example_group_spec.rb b/spec/rspec/core/shared_example_group_spec.rb index 3512c96647..db95bd76b5 100644 --- a/spec/rspec/core/shared_example_group_spec.rb +++ b/spec/rspec/core/shared_example_group_spec.rb @@ -56,16 +56,7 @@ def find_implementation_block(registry, scope, name) registry.find([scope], name).definition end - it "is exposed to the global namespace when expose_dsl_globally is enabled" do - in_sub_process do - RSpec.configuration.expose_dsl_globally = true - expect(Kernel).to respond_to(shared_method_name) - end - end - - it "is not exposed to the global namespace when monkey patching is disabled" do - RSpec.configuration.expose_dsl_globally = false - expect(RSpec.configuration.expose_dsl_globally?).to eq(false) + it "is not exposed to the global namespace" do expect(Kernel).to_not respond_to(shared_method_name) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 20e84ed320..0393bcfff3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -73,7 +73,6 @@ def handle_current_dir_change # structural c.alias_it_behaves_like_to 'it_has_behavior' c.include(RSpecHelpers) - c.disable_monkey_patching! # runtime options c.raise_errors_for_deprecations!