Skip to content

Commit

Permalink
Delegate to private methods
Browse files Browse the repository at this point in the history
Issue #526
Previously if a class delegated to a private method the `matches?` method would
return `false`. This was because`respond_to?` always returns `false` for private methods, this commit fixes this by making `respond_to?` evaluate private methods.
  • Loading branch information
Adam89 committed Jun 14, 2014
1 parent f13e069 commit 348a730
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/shoulda/matchers/independent/delegate_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def subject_has_delegating_method?
end

def subject_has_target_method?
subject.respond_to?(target_method)
subject.respond_to?(target_method, true)
end

def ensure_target_method_is_present!
Expand Down
35 changes: 35 additions & 0 deletions spec/shoulda/matchers/independent/delegate_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,41 @@ def mailman
end
end

context 'given a private method that delegates properly' do
before do
define_class(:mailman) do
def deliver_mail
end
end

define_class(:post_office) do
def deliver_mail
mailman.deliver_mail
end

def mailman
Mailman.new
end

private :mailman
end
end

it 'accepts' do
post_office = PostOffice.new
expect(post_office).to delegate_method(:deliver_mail).to(:mailman)
end

it 'produces the correct failure message if the assertion was negated' do
post_office = PostOffice.new
message = 'Expected PostOffice#deliver_mail not to delegate to PostOffice#mailman, but it did'

expect {
expect(post_office).not_to delegate_method(:deliver_mail).to(:mailman)
}.to fail_with_message(message)
end
end

context 'given a method that delegates properly with arguments' do
before do
define_class(:mailman)
Expand Down

0 comments on commit 348a730

Please sign in to comment.