diff --git a/Changelog.md b/Changelog.md index 0700a7b42b..e403599e4c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -3,7 +3,9 @@ Breaking Changes: * Ruby < 2.3 is no longer supported. (Phil Pirozhkov, #2787) -* Remove monkey-patching syntax. (Phil Pirozhkov, #2803) +* 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/subject/one_liner_syntax.feature b/features/subject/one_liner_syntax.feature index f9fe4b810e..fdcedfc236 100644 --- a/features/subject/one_liner_syntax.feature +++ b/features/subject/one_liner_syntax.feature @@ -1,19 +1,13 @@ 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. + understanding the object you are describing. This syntax is a shorthand for + `expect(subject)`. Notes: @@ -26,8 +20,6 @@ Feature: One-liner syntax """ruby RSpec.describe Array do describe "when first created" do - it { should be_empty } - # or it { is_expected.to be_empty } end end @@ -39,7 +31,6 @@ Feature: One-liner syntax Array when first created is expected to be empty - is expected to be empty """ Scenario: Explicit subject @@ -48,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 @@ -61,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/lib/rspec/core/memoized_helpers.rb b/lib/rspec/core/memoized_helpers.rb index fbf932f410..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,23 +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 - # should be_eligible_to_vote + # 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 - # should be_eligible_to_vote + # 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 @@ -49,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 @@ -59,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. # @@ -109,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 @@ -410,8 +363,6 @@ def let!(name, &block) # end # end # - # @see MemoizedHelpers#should - # @see MemoizedHelpers#should_not # @see MemoizedHelpers#is_expected def subject(name=nil, &block) if name 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