README

Path: README
Last Update: Fri Jul 20 17:38:40 -0400 2007

Handoff provides a fluent interface for testing simple delegation. The Ruby stdlib module Forwardable makes implementing delegation simple and readable. Handoff is intended to make the code to test it just as simple and readable.

You can install the latest gem using gem install handoff or download the .gem file from rubyforge.org/projects/handoff.

As of 0.2.0, Handoff no longer depends on Mocha and Stubba.

Here‘s example_test.rb to give you an idea of what you can do:

 # This example is only helpful if you're familiar with Forwardable from the
 # stdlib, which you should be if you're coding delegating behavior in Ruby.
 #
 # Foo is a simple delegating class.  We want to specify that delegation in
 # its unit test.
 class Foo
   require 'forwardable'
   extend Forwardable
   def_delegators :bar, :baz, :bat
   def_delegator :bar, :baq, :bar_baq
   def_delegator :bar, :ban, :bar_ban

   # Foo#bar gets the delegate.  This is not what Handoff tests.
   # Rather, Handoff will mock this method and ensure that the
   # delegating methods defined above use it correctly.
   def bar
     @bar ||= BarFactory.create
   end
 end

 require 'test/unit'

 require File.expand_path(File.dirname(__FILE__) + '/../lib/handoff') # I require it this way because I don't want the installed gem.
 # require 'handoff' # This is how you'd require handoff (or do require_gem w/ a version).

 class ExampleTest < Test::Unit::TestCase

   def test_using_from_and_single_method
     assert_handoff.from(Foo.new).to(:bar).for_method(:baz)
   end

   def test_using_from_any_and_multiple_methods
     assert_handoff.from_any(Foo).to(:bar).for_methods(:baz, :bat)
   end

   def test_showing_delegating_methods_with_names_different_than_their_delegate_methods
     assert_handoff.from(Foo.new).to(:bar).for(:bar_baq => :baq, :bar_ban => :ban)
   end

   def test_specifying_args
     assert_handoff.from(Foo.new).to(:bar).for(:baz).with('arg1', 2, :three)
   end

   def test_specifying_some_arg
     assert_handoff.from(Foo.new).to(:bar).for(:baz).with_no_arguments
   end

 end

[Validate]