[rspec-users] DelegateClass fails

John Prince jtprince at gmail.com
Mon Jun 11 15:42:14 EDT 2007


This is not a big deal, but DelegateClass fails with rspec when methods 
are defined in the the delegating class.  Here's code that demonstrates:

require 'delegate'
gem 'rspec'
# Version 1.0.5

############################################################
# CONTROL EXAMPLE: Works FINE.
############################################################
class InheritedClass < Array
  def large?
    self.size >= 5
  end
end

describe InheritedClass, "normal inheriting class is OK" do
  it 'works just fine' do
    myclass = InheritedClass.new([1,2,3,4,5,6])
    myclass.should be_large
  end
end


############################################################
# rspec should work on DelegateClasses.  Not sure how...
############################################################

class DelegateKlass < DelegateClass(Array)

  def initialize(array)
    @internal_array = array
    super(@internal_array)
  end

  def large?
    @internal_array.size >= 5
  end

end

=begin
# will work if this code is available.
class Array
  def large?
    self.size >= 5
  end
end
=end


describe DelegateKlass, "delegate class fails" do
  it 'does not work on methods defined within the delegating class' do
    myclass = DelegateKlass.new([1,2,3,4,5,6])
    myclass.should be_large
  end
end


--JTP


More information about the rspec-users mailing list