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 wouldn’t want behaviour like this myself unless it could be invoked
explicitly with a command line argument, but was otherwise off. In
other words, you could do something like:
Most people ask for this kind of feature from their unit tests because
they’re not combining mocking unit tests with end-to-end acceptance
tests. Are you using both?
Yes, your reading of the request is what I originally meant.
I appreciate the points made by others in this thread and the thread
David has referred to.
As I continue to learn RSpec I will undoubtedly avail myself of the
approaches recommended above or in the linked thread, but I think an
argument to stub() per the following could be useful:
f.stub(:barr, :throw_no_such_method_error=>true)
#=> Error: “The Foo class does not have a ‘barr’ method. Perhaps you
meant to stub ‘bar’”
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: