Skip to content

Commit

Permalink
Add note to use no args explicitly with not_to.
Browse files Browse the repository at this point in the history
Pass the first non-RSpec lib call site instead of defaulting to the 3rd
site, due to the deeper callstack from the block form of `expect`.
  • Loading branch information
cupakromer authored and myronmarston committed Aug 28, 2013
1 parent 8594c94 commit a1b300c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/rspec/expectations/deprecation.rb
Expand Up @@ -5,9 +5,12 @@ module Deprecation
#
# Used internally to print deprecation warnings
def deprecate(deprecated, options={})
call_site = caller.find { |line| line !~ %r{/lib/rspec/(core|mocks|expectations|matchers|rails)/} }
call_site ||= caller(0)[2]

message = "DEPRECATION: #{deprecated} is deprecated."
message << " Use #{options[:replacement]} instead." if options[:replacement]
message << " Called from #{caller(0)[2]}."
message << " Called from #{call_site}."
warn message
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/rspec/matchers/built_in/raise_error.rb
Expand Up @@ -22,7 +22,11 @@ def matches?(given_proc, negative_expectation = false)
elsif @expected_message
"`expect { }.not_to raise_error(message)`"
end
RSpec.deprecate(what_to_deprecate, :replacement => "`expect { }.not_to raise_error()`")

RSpec.deprecate(
what_to_deprecate,
:replacement => "`expect { }.not_to raise_error()` (with no args)"
)
end
@raised_expected_error = false
@with_expected_message = false
Expand Down
56 changes: 53 additions & 3 deletions spec/rspec/matchers/raise_error_spec.rb
@@ -1,5 +1,18 @@
require 'spec_helper'

module WrapDeprecationCallSite
def expect_deprecation_with_call_site(file, line)
actual_call_site = nil
allow(RSpec.configuration.reporter).to receive(:deprecation) do |options|
actual_call_site = options[:call_site]
end

yield

expect(actual_call_site).to match([file, line].join(':'))
end
end

describe "expect { ... }.to raise_error" do
it_behaves_like("an RSpec matcher", :valid_value => lambda { raise "boom" },
:invalid_value => lambda { }) do
Expand Down Expand Up @@ -146,15 +159,28 @@
end

describe "expect { ... }.not_to raise_error(message)" do
include WrapDeprecationCallSite

before do
allow(RSpec).to receive(:deprecate)
end

it "is deprecated" do
expect(RSpec).to receive(:deprecate).with(/not_to raise_error\(message\)/, :replacement =>"`expect { }.not_to raise_error()`")
expect(RSpec).to receive(:deprecate).with(
/not_to raise_error\(message\)/,
:replacement =>"`expect { }.not_to raise_error()` (with no args)"
)
expect {raise 'blarg'}.not_to raise_error('blah')
end

it 'reports the line number of the deprecated syntax' do
allow(RSpec).to receive(:deprecate).and_call_original

expect_deprecation_with_call_site(__FILE__, __LINE__ + 1) do
expect {raise 'blarg'}.not_to raise_error('blah')
end
end

it "passes if RuntimeError error is raised with the different message" do
expect {raise 'blarg'}.not_to raise_error('blah')
end
Expand Down Expand Up @@ -201,15 +227,27 @@
end

describe "expect { ... }.not_to raise_error(NamedError)" do
include WrapDeprecationCallSite

before do
allow(RSpec).to receive(:deprecate)
end

it "is deprecated" do
expect(RSpec).to receive(:deprecate).with(/not_to raise_error\(SpecificErrorClass\)/, :replacement => "`expect { }.not_to raise_error()`")
expect(RSpec).to receive(:deprecate).with(
/not_to raise_error\(SpecificErrorClass\)/,
:replacement =>"`expect { }.not_to raise_error()` (with no args)"
)
expect { }.not_to raise_error(NameError)
end

it 'reports the line number of the deprecated syntax' do
allow(RSpec).to receive(:deprecate).and_call_original
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1) do
expect { }.not_to raise_error(NameError)
end
end

it "passes if nothing is raised" do
expect { }.not_to raise_error(NameError)
end
Expand Down Expand Up @@ -250,15 +288,27 @@
end

describe "expect { ... }.not_to raise_error(NamedError, error_message) with String" do
include WrapDeprecationCallSite

before do
allow(RSpec).to receive(:deprecate)
end

it "is deprecated" do
expect(RSpec).to receive(:deprecate).with(/not_to raise_error\(SpecificErrorClass, message\)/, :replacement =>"`expect { }.not_to raise_error()`")
expect(RSpec).to receive(:deprecate).with(
/not_to raise_error\(SpecificErrorClass, message\)/,
:replacement =>"`expect { }.not_to raise_error()` (with no args)"
)
expect {}.not_to raise_error(RuntimeError, "example message")
end

it 'reports the line number of the deprecated syntax' do
allow(RSpec).to receive(:deprecate).and_call_original
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1) do
expect {}.not_to raise_error(RuntimeError, "example message")
end
end

it "passes if nothing is raised" do
expect {}.not_to raise_error(RuntimeError, "example message")
end
Expand Down

0 comments on commit a1b300c

Please sign in to comment.