diff --git a/lib/rspec/matchers/built_in/has.rb b/lib/rspec/matchers/built_in/has.rb index 4973893c4..2040a3416 100644 --- a/lib/rspec/matchers/built_in/has.rb +++ b/lib/rspec/matchers/built_in/has.rb @@ -78,7 +78,8 @@ def predicate_matches?(value=true) predicate_actual = predicate_result if value == !!predicate_actual && value != predicate_actual RSpec.deprecate( - "`#{predicate_method_name}` returned neither `true` nor `false`, but rather `#{predicate_actual.inspect}`") + "`#{predicate_method_name}` returned neither `true` nor `false`, but rather `#{predicate_actual.inspect}`", + :replacement => "`expect(subject.#{predicate_method_name}).to #{value ? "be_truthy" : "be_falsey"}`") end value == !!predicate_actual end diff --git a/spec/rspec/matchers/built_in/be_spec.rb b/spec/rspec/matchers/built_in/be_spec.rb index 55443c751..1be745503 100644 --- a/spec/rspec/matchers/built_in/be_spec.rb +++ b/spec/rspec/matchers/built_in/be_spec.rb @@ -93,11 +93,22 @@ }.to fail_with("expected `#{actual.inspect}.happy?` to be truthy, got false") end - it "prints a deprecation warning" do - expect(RSpec). - to receive(:deprecate). - with("`infinite?` returned neither `true` nor `false`, but rather `-1`") - expect(-Float::INFINITY).to be_infinite + context "when the predicate neither returns true or false" do + it "prints a deprecation warning when actual is truthy" do + expect(RSpec). + to receive(:deprecate). + with("`infinite?` returned neither `true` nor `false`, but rather `-1`", + :replacement => "`expect(subject.infinite?).to be_truthy`") + expect(-Float::INFINITY).to be_infinite + end + + it "prints a deprecation warning when actual is truthy" do + expect(RSpec). + to receive(:deprecate). + with("`infinite?` returned neither `true` nor `false`, but rather `nil`", + :replacement => "`expect(subject.infinite?).to be_falsey`") + expect(1).to_not be_infinite + end end end diff --git a/spec/rspec/matchers/built_in/has_spec.rb b/spec/rspec/matchers/built_in/has_spec.rb index 98975818a..7ff0699e6 100644 --- a/spec/rspec/matchers/built_in/has_spec.rb +++ b/spec/rspec/matchers/built_in/has_spec.rb @@ -160,12 +160,24 @@ def o.has_sym?(sym); sym == :foo; end expect(actual).not_to have_foo end - it "prints a deprecation warning" do - expect(RSpec). - to receive(:deprecate). - with("`has_foo?` returned neither `true` nor `false`, but rather `nil`") - actual = double("actual", :has_foo? => nil) - expect(actual).not_to have_foo + context "when the predicate neither returns true or false" do + it "prints a deprecation warning when actual is falsey" do + expect(RSpec). + to receive(:deprecate). + with("`has_foo?` returned neither `true` nor `false`, but rather `nil`", + :replacement => "`expect(subject.has_foo?).to be_falsey`") + actual = double("actual", :has_foo? => nil) + expect(actual).not_to have_foo + end + + it "prints a deprecation warning when actual is truthy" do + expect(RSpec). + to receive(:deprecate). + with('`has_foo?` returned neither `true` nor `false`, but rather `"bar"`', + :replacement => "`expect(subject.has_foo?).to be_truthy`") + actual = double("actual", :has_foo? => "bar") + expect(actual).to have_foo + end end end