Permalink
Browse files

Respond to feedback.

  • Loading branch information...
1 parent 2377fb4 commit a117db9ff7839f6c43da75229127cc97eee9c9d0 @readeharris readeharris committed with drapergeek Oct 11, 2012
@@ -2,7 +2,7 @@
module Shoulda
module Matchers
- # = Matchers for plain ol' Ruby objects.
+ # = Matchers for non-Rails-related code.
module Independent
end
end
@@ -4,6 +4,10 @@ module Independent # :nodoc:
# Ensure that a given method is delegated properly.
#
+ # Dependencies:
+ # This matcher requires the `bourne` gem be added to your Gemfile. You will receive
+ # a warning if this is not the case.
+ #
# Basic Syntax:
# it { should delegate_method(:deliver_mail).to(:mailman) }
#
@@ -13,7 +17,7 @@ module Independent # :nodoc:
#
# Examples:
# it { should delegate_method(:deliver_mail).to(:mailman).as(:deliver_with_haste)
- # it { should delegate_method(:deliver_mail).to(:mailman).with_arguments("221B Baker St.", speed: :presently)
+ # it { should delegate_method(:deliver_mail).to(:mailman).with_arguments("221B Baker St.", :hastily => true)
#
def delegate_method(delegating_method)
require 'bourne'
@@ -71,15 +75,15 @@ def failure_message
private
def add_clarifications_to(message)
- message.tap do |message|
- if @delegated_arguments.present?
- message.concat(" with arguments: #{@delegated_arguments}")
- end
-
- if @method_on_target.present?
- message.concat(" as :#{@method_on_target}")
- end
+ if @delegated_arguments.present?
+ message << " with arguments: #{@delegated_arguments}"
+ end
+
+ if @method_on_target.present?
+ message << " as :#{@method_on_target}"
end
+
+ message
end
def delegating_method_name
@@ -103,7 +107,9 @@ def method_on_target
end
def ensure_target_method_is_present!
- raise TargetNotDefinedError unless @target_method.present?
+ if @target_method.blank?
+ raise TargetNotDefinedError
+ end
end
end
@@ -1,7 +1,7 @@
# :enddoc:
require 'test/unit/testcase'
-
require 'shoulda/matchers/independent'
+
module Test
module Unit
class TestCase
@@ -32,35 +32,38 @@
context 'given a method that does not delegate' do
before do
- class PostOffice
+ define_class(:post_office) do
def deliver_mail
:delivered
end
end
end
- it 'fails with a useful message' do
- begin
- post_office = PostOffice.new
- post_office.should delegate_method(:deliver_mail).to(:mailman)
- rescue Exception => e
- e.message.should == 'Expected PostOffice#deliver_mail to delegate to PostOffice#mailman'
- end
+ it 'does not match' do
+ post_office = PostOffice.new
+ matcher = delegate_method(:deliver_mail).to(:mailman)
+ matcher.matches?(post_office).should be_false
+ end
+
+ it 'has a failure message that indicates which method should have been delegated' do
+ post_office = PostOffice.new
+ matcher = delegate_method(:deliver_mail).to(:mailman)
+ matcher.matches?(post_office)
+ matcher.failure_message.should == 'Expected PostOffice#deliver_mail to delegate to PostOffice#mailman'
end
it 'uses the proper syntax for class methods in errors' do
- begin
- PostOffice.should delegate_method(:deliver_mail).to(:mailman)
- rescue => e
- e.message.should == 'Expected PostOffice.deliver_mail to delegate to PostOffice.mailman'
- end
+ post_office = PostOffice.new
+ matcher = delegate_method(:deliver_mail).to(:mailman)
+ matcher.matches?(PostOffice)
+ matcher.failure_message.should == 'Expected PostOffice.deliver_mail to delegate to PostOffice.mailman'
end
end
context 'given a method that delegates properly' do
before do
- class Mailman; end
- class PostOffice
+ define_class(:mailman)
+ define_class(:post_office) do
def deliver_mail
mailman.deliver_mail
end
@@ -71,18 +74,18 @@ def mailman
end
end
- it 'succeeds' do
+ it 'matches' do
post_office = PostOffice.new
post_office.should delegate_method(:deliver_mail).to(:mailman)
end
end
context 'given a method that delegates properly with certain arguments' do
before do
- class Mailman; end
- class PostOffice
+ define_class(:mailman)
+ define_class(:post_office) do
def deliver_mail
- mailman.deliver_mail("221B Baker St.", speed: :presently)
+ mailman.deliver_mail("221B Baker St.", hastily: true)
end
def mailman
@@ -92,32 +95,33 @@ def mailman
end
context 'when given the correct arguments' do
- it 'succeeds' do
+ it 'matches' do
post_office = PostOffice.new
- post_office.should delegate_method(:deliver_mail)
- .to(:mailman)
- .with_arguments("221B Baker St.", speed: :presently)
+ matcher = delegate_method(:deliver_mail).to(:mailman).with_arguments("221B Baker St.", hastily: true)
+ post_office.should matcher
end
end
context 'when not given the correct arguments' do
- it 'fails with a useful message' do
- begin
- post_office = PostOffice.new
- post_office.should delegate_method(:deliver_mail)
- .to(:mailman)
- .with_arguments("123 Nowhere Ln.")
- rescue Exception => e
- e.message.should == 'Expected PostOffice#deliver_mail to delegate to PostOffice#mailman with arguments: ["123 Nowhere Ln."]'
- end
+ it 'does not match' do
+ post_office = PostOffice.new
+ matcher = delegate_method(:deliver_mail).to(:mailman).with_arguments("123 Nowhere Ln.")
+ matcher.matches?(post_office).should be_false
+ end
+
+ it 'has a failure message that indicates which arguments were expected' do
+ post_office = PostOffice.new
+ matcher = delegate_method(:deliver_mail).to(:mailman).with_arguments("123 Nowhere Ln.")
+ matcher.matches?(post_office)
+ matcher.failure_message.should == 'Expected PostOffice#deliver_mail to delegate to PostOffice#mailman with arguments: ["123 Nowhere Ln."]'
end
end
end
context 'given a method that delegates properly to a method of a different name' do
before do
- class Mailman; end
- class PostOffice
+ define_class(:mailman)
+ define_class(:post_office) do
def deliver_mail
mailman.deliver_mail_and_avoid_dogs
end
@@ -129,24 +133,25 @@ def mailman
end
context 'when given the correct method name' do
- it 'succeeds' do
+ it 'matches' do
post_office = PostOffice.new
- post_office.should delegate_method(:deliver_mail)
- .to(:mailman)
- .as(:deliver_mail_and_avoid_dogs)
+ matcher = delegate_method(:deliver_mail).to(:mailman).as(:deliver_mail_and_avoid_dogs)
+ post_office.should matcher
end
end
context 'when given an incorrect method name' do
- it 'fails with a useful message' do
- begin
- post_office = PostOffice.new
- post_office.should delegate_method(:deliver_mail)
- .to(:mailman)
- .as(:deliver_mail_without_regard_for_safety)
- rescue Exception => e
- e.message.should == "Expected PostOffice#deliver_mail to delegate to PostOffice#mailman as :deliver_mail_without_regard_for_safety"
- end
+ it 'does not match' do
+ post_office = PostOffice.new
+ matcher = delegate_method(:deliver_mail).to(:mailman).as(:watch_tv)
+ matcher.matches?(post_office).should be_false
+ end
+
+ it 'has a failure message that indicates which method was expected' do
+ post_office = PostOffice.new
+ matcher = delegate_method(:deliver_mail).to(:mailman).as(:watch_tv)
+ matcher.matches?(post_office)
+ matcher.failure_message.should == 'Expected PostOffice#deliver_mail to delegate to PostOffice#mailman as :watch_tv'
end
end
end

0 comments on commit a117db9

Please sign in to comment.