[rspec-users] newbie: how to preserve NoMethodError under stubbing?

David Chelimsky dchelimsky at gmail.com
Sat Jul 24 07:15:59 EDT 2010


On Jul 24, 2010, at 4:35 AM, Wincent Colaiuta wrote:

> El 24/07/2010, a las 08:26, David Chelimsky escribió:
> 
>> On Fri, Jul 23, 2010 at 6:46 PM, Lille <lille.penguini at gmail.com> wrote:
>>> Hi,
>>> 
>>> I've been browsing the RSpec book and the RDoc, but I can't see how to
>>> ensure the following:
>>> 
>>> Stub an instance with a method it doesn't have and raise NoMethodError
>>> (or something like it.)
>> 
>> RSpec doesn't support anything like that. I'm not sure if any of the
>> Ruby frameworks do, though I've been involved with conversations about
>> this sort of thing before.
> 
> I'm not sure if I understand the request, but with RR you can do this:
> 
>>> require 'rr'
> => true
>>> extend RR::Adapters::RRMethods
> => main
>>> foo = Object.new
> => #<Object:0x101664bd8>
>>> stub(foo).bar { raise NoMethodError }
> => #<RR::DoubleDefinitions::DoubleDefinition:0x10165ce60>
>>> foo.bar
> NoMethodError: NoMethodError
> 
> Although like I said, not sure if I understood Lille's request.

I read the request as this:

class Foo
  def bar; end
end

f = Foo.new
f.stub(:barr)
#=> Error: "The Foo class does not have a 'barr' method. Perhaps you meant to stub 'bar'"

For me this would have very limited utility because 1/2 the time I'm deliberately stubbing methods that don't even exist yet on doubles doubling for objects that don't even exist yet, which is why I wouldn't want it to be something that happens implicitly by default.

Here's another thread on the same matter from a little over a year ago:

http://groups.google.com/group/rspec/browse_thread/thread/cf0b3eae192b9d8c/f5b77b008de628c7?lnk=gst&q=mock+audit#f5b77b008de628c7

Cheers,
David

> 
> Cheers,
> Wincent



More information about the rspec-users mailing list