[rspec-users] [bug] mock expectations fail when a subclass is instantiated

Chuck Remes cremes.devlist at mac.com
Sat Aug 16 17:25:28 EDT 2008


I took a look at the web site but did not see any issue/bug tracker,  
so I'm reporting this here.

The mock framework fails to differentiate between messages sent to a  
parent class versus a subclass, so some of my expectations fail when I  
assert some expectation on a parent. Here's code to show it:

cremes$ cat c.rb
class A
end

class B < A
end

class C
   def foo
     a = A.new
     b = B.new
   end
end


cremes$ cat c_spec.rb
require 'rubygems'
require 'spec'
require 'c'

describe C, "mock" do
   it "should incorrectly pick up message sent to parent class as a  
subclass err" do
     A.should_receive(:new).once
     c = C.new
     c.foo
   end
end


cremes$ spec c_spec.rb
F

1)
NoMethodError in 'C mock should incorrectly pick up message sent to  
parent class as a subclass err'
undefined method `new' for B:Class
./c.rb:10:in `foo'
./c_spec.rb:9:

Finished in 0.006664 seconds

1 example, 1 failure


If I capture the exception thrown here, it is at lib/spec/mocks/ 
proxy.rb line 75. I'd provide a patch if I knew how to fix it! :)

cr



More information about the rspec-users mailing list