rspec/rspec-core — Issue #471: be_* predicate gives wrong results when requiring delegate before rspec
Author: eregon
Created: 2011-10-08 16:36:44+00:00
State: open

This bug is really weird, I searched what caused it during a few hours.

It is probably similar to: http://rubyforge.org/tracker/?func=detail&atid=3149&aid=11526&group_id=797
I tried to find more info about it but did not succeed.
Could you give me access to http://rspec.lighthouseapp.com/projects/5645-rspec/tickets/48-11526-rspec-fails-to-dynamically-bind-delegateclass-methods#ticket-48-2 ?

Here is a small reproducing example:

``` ruby
# compare `RUBYOPT=-rdelegate rspec spec.rb` and `rspec spec.rb`
require 'delegate'

class Immediate
def cached?
false
end
end

class Cached < SimpleDelegator
def initialize(d)
super d
end

def cached?
true
end
end

describe 'A' do
subject{ Cached.new(Immediate.new) }

it 'b' do
p subject.cached? # => true
subject.should be_cached
end
end

```
The example fails only when the 'delegate' library is required before 'rspec', on 1.9 (jruby and mri).
You can also observe this problem by running the specs of rspec-expectations:

RUBYOPT=-rdelegate rake spec

The "arbitrary predicate with DelegateClass"/"accesses methods defined in the delegating class (LH[#48])" (spec/rspec/matchers/be_spec.rb#412) example is indeed testing similar behavior.

I guess the problem mainly lies in `lib/delegate.rb` which changed a lot from 1.8 on 1.9, notably by using BasicObject. There is some dark magic there, like `Kernel.dup`.

Until the problem is found, I think RSpec should emit a warning (or abort) when 'delegate' is required before 'rspec' to help users hitting this bug.

What do you think?