Skip to content

Commit

Permalink
[expectations] Merge pull request rspec/rspec-expectations#521 from d…
Browse files Browse the repository at this point in the history
…anielfone/2-99-maintenance

Deprecation warning on Has matcher

---
This commit was imported from rspec/rspec-expectations@9e0418f.
  • Loading branch information
myronmarston committed Apr 10, 2014
2 parents 9f32572 + d25bd41 commit 1d5bbae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions rspec-expectations/Changelog.md
Expand Up @@ -11,6 +11,8 @@ Deprecations:
`RSpec::Expectations::Configuration`. (Myron Marston)
* Deprecate `be_xyz` predicate matcher on an object that doesn't respond to
`xyz?` or `xyzs?`. (Daniel Fone)
* Deprecate `have_xyz` matcher on an object that doesn't respond to `has_xyz?`.
(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
12 changes: 11 additions & 1 deletion rspec-expectations/lib/rspec/matchers/built_in/has.rb
Expand Up @@ -9,7 +9,10 @@ def initialize(expected, *args)
end

def matches?(actual)
actual.__send__(predicate(@expected), *@args)
method = predicate(@expected)
result = actual.__send__(method, *@args)
check_respond_to(actual, method)
result
end

def failure_message_for_should
Expand Down Expand Up @@ -43,6 +46,13 @@ def failure_message_args_description
desc = args_description
"(#{desc})" if desc
end

def check_respond_to(actual, method)
RSpec.deprecate(
"Matching with #{@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
10 changes: 10 additions & 0 deletions rspec-expectations/spec/rspec/matchers/has_spec.rb
Expand Up @@ -57,6 +57,16 @@ def o.has_sym?(*args)
expect(o).to have_sym(:foo)
}.to raise_error("Funky exception")
end

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

describe "expect(...).not_to have_sym(*args)" do
Expand Down

0 comments on commit 1d5bbae

Please sign in to comment.