[rspec-devel] [ rspec-Bugs-11526 ] rspec fails to dynamically bind DelegateClass methods

noreply at rubyforge.org noreply at rubyforge.org
Fri Sep 7 06:05:46 EDT 2007


Bugs item #11526, was opened at 2007-06-12 15:55
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=11526&group_id=797

Category: None
Group: None
Status: Open
Resolution: None
Priority: 3
Submitted By: John Prince (jtprince)
Assigned to: Nobody (None)
Summary: rspec fails to dynamically bind DelegateClass methods

Initial Comment:
DelegateClass fails with rspec when methods are defined in the the delegating class.  I'm using a delegate class right now and I'm having to write spaghetti code to get things to work properly.  Here's code that demonstrates the problem:

---
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

---

#############################################
# OUTPUT:
#############################################

.F

1)
NoMethodError in 'DelegateKlass delegate class fails does not work on 
delegate class'
undefined method `large?' for [1, 2, 3, 4, 5, 6]:Array
./spec_delegate.rb:53:

Finished in 0.007365 seconds

2 examples, 1 failure

----------------------------------------------------------------------

>Comment By: David Chelimsky (dchelimsky)
Date: 2007-09-07 10:05

Message:
Well, I've learned one interesting thing about this. If you define the DelegateKlass first and then require 'spec' (or gem 'rspec') it works fine. So there is some odd relationship between what require 'spec' does to Object or the main object and what DelegateClass does.

Don't know more than that yet.

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=11526&group_id=797


More information about the rspec-devel mailing list