Skip to content

Commit

Permalink
[expectations] Merge pull request rspec/rspec-expectations#519 from d…
Browse files Browse the repository at this point in the history
…anielfone/deprecate-blind-predicate-calls

Deprecation warning on predicate matcher

---
This commit was imported from rspec/rspec-expectations@56274d7.
  • Loading branch information
myronmarston committed Apr 4, 2014
2 parents 42c008b + d92916d commit d4c822e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions rspec-expectations/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Deprecations:
* Deprecate `RSpec::Matchers::Pretty#expected_to_sentence`. (Myron Marston)
* Deprecate `RSpec::Matchers::Configuration` in favor of
`RSpec::Expectations::Configuration`. (Myron Marston)
* Deprecate `be_xyz` predicate matcher on an object that doesn't respond to
`xyz?` or `xyzs?`. (Daniel Fone)

### 2.99.0.beta2 / 2014-02-17
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.99.0.beta1...v2.99.0.beta2)
Expand Down
15 changes: 13 additions & 2 deletions rspec-expectations/lib/rspec/matchers/built_in/be.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,17 @@ def matches?(actual)
end

begin
return @result = actual.__send__(predicate, *@args, &@block)
@result = actual.__send__(predicate, *@args, &@block)
check_respond_to(predicate)
return @result
rescue NameError => predicate_missing_error
"this needs to be here or rcov will not count this branch even though it's executed in a code example"
end

begin
return @result = actual.__send__(present_tense_predicate, *@args, &@block)
@result = actual.__send__(present_tense_predicate, *@args, &@block)
check_respond_to(present_tense_predicate)
return @result
rescue NameError
raise predicate_missing_error
end
Expand Down Expand Up @@ -209,6 +213,13 @@ def prefix_and_expected(symbol)
def prefix_to_sentence
split_words(@prefix)
end

def check_respond_to(method)
RSpec.deprecate(
"Matching with #{@prefix}#{@expected} on an object that doesn't respond to `#{method}`",
:replacement => "`respond_to_missing?` or `respond_to?` on your object"
) unless actual.respond_to?(method)
end
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions rspec-expectations/spec/rspec/matchers/be_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def happy?
true
end
end
allow(RSpec).to receive(:deprecate)
expect(RSpec).to receive(:deprecate).with(
"matching with be_happy on private method happy?",
:replacement => "`expect(object.send(:happy?)).to be_true` or change the method's visibility to public",
Expand Down Expand Up @@ -67,6 +68,26 @@ def happy?
value = double(:happy? => false)
expect(matcher == value).to be false
end

it 'warns of deprecation when actual does not respond to :predicate?' do
oddly_happy_class = Class.new do
def method_missing(method)
return true if method == :happy?
end
end
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1, /Matching with be_happy on an object that doesn't respond to `happy\?`/)
expect(oddly_happy_class.new).to be_happy
end

it 'does not warn of deprecation when actual responds to present tense predicate' do
present_happy_class = Class.new do
def exists?
true
end
end
expect_no_deprecation
expect(present_happy_class.new).to be_exist
end
end

describe "expect(...).not_to be_predicate" do
Expand Down

0 comments on commit d4c822e

Please sign in to comment.