Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle dynamic mocks specially for null-object doubles #1455

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/rspec/matchers/built_in/has.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,22 @@ def description

private

# Catch a semi-frequent typo - if you have strict_predicate_matchers disabled and
# expect(spy).to have_receieveddd(:foo) it would be evergreen - the dynamic matcher
# queries `has_receiveddd?`, the spy _fakes_ the method, returning its (truthy) self.
def really_responds_to?(method)
if defined?(RSpec::Mocks::Double) && @actual.is_a?(RSpec::Mocks::Double)
nevinera marked this conversation as resolved.
Show resolved Hide resolved
@actual.respond_to?(method) && @actual.methods.include?(method)
else
@actual.respond_to?(method)
end
rescue NoMethodError
# Proxied BasicObjects don't have `is_a?`, or basically anything else.
@actual.respond_to?(method)
end

def predicate_accessible?
@actual.respond_to? predicate
really_responds_to?(predicate)
end

# support 1.8.7, evaluate once at load time for performance
nevinera marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -155,7 +169,7 @@ def failure_to_respond_explanation
end

def predicate_accessible?
super || actual.respond_to?(present_tense_predicate)
super || really_responds_to?(present_tense_predicate)
end

def present_tense_predicate
Expand Down
14 changes: 14 additions & 0 deletions spec/rspec/matchers/built_in/be_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@
expect(actual).to be_happy
}.to fail_with("expected `#{actual.inspect}.happy?` to be truthy, got false")
end

it "allows dynamic matchers to pass for supplied methods on spies" do
thing = spy("thing", :has_recv? => true)
expect(thing).to have_recv(:foo)
expect(thing).to have_received(:has_recv?).with(:foo)
nevinera marked this conversation as resolved.
Show resolved Hide resolved
end

it "does not allow dynamic matchers to pass for inferred methods on spies" do
thing = spy("thing")
expect {
expect(thing).to have_recv(:foo)
}.to fail
expect(thing).not_to have_received(:has_recv?)
end
end

it "fails when actual does not respond to :predicate?" do
Expand Down
14 changes: 14 additions & 0 deletions spec/rspec/matchers/built_in/has_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ def o.has_sym?(sym); sym == :foo; end
actual = double("actual", :has_foo? => nil)
expect(actual).not_to have_foo
end

it "allows dynamic matchers to pass for supplied methods on spies" do
thing = spy("thing", :furg? => true)
expect(thing).to be_furg(:foo)
expect(thing).to have_received(:furg?).with(:foo)
end

it "does not allow dynamic matchers to pass for inferred methods on spies" do
thing = spy("thing")
expect {
expect(thing).to be_furg(:foo)
}.to fail
expect(thing).not_to have_received(:furg?)
end
end

it "fails if #has_sym?(*args) returns true" do
Expand Down
Loading