# Is there a "safe\_stub"?

**URL:** <https://www.ruby-forum.com/t/is-there-a-safe-stub/209995>\
**Category:** RSpec\
**Created:** [July 27, 2011, 9:56pm UTC](https://www.ruby-forum.com/t/is-there-a-safe-stub/209995 "2011-07-27T21:56:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [July 27, 2011, 9:56pm UTC](https://www.ruby-forum.com/t/is-there-a-safe-stub/209995/1 "2011-07-27T21:56:28Z")

</div>

Right now, #stub stubs a method even if it doesn’t exist. Is there a  
way to stub a method on an object, if and only if it already  
#responds\_to? that method? It would also be good if it raised an  
ArgumentError or the like.

Example:

# this is how it works now:

# - stubs the #bar method even if it doesn’t exist

# - always returns ‘baz’

foo.stub(:bar).and\_return(‘baz’)

What I’d like is something like this:

# raise ArgumentError unless foo.respond\_to? :bar

# otherwise, behaves as the regular #stub does

foo.stub!(:bar).and\_return(‘baz’)

Thoughts?

## ~ jf

John F.  
Principal Consultant, BitsBuilder  
LI: [http://www.linkedin.com/in/johnxf](http://www.linkedin.com/in/johnxf)  
SO: [User John Feminella - Stack Overflow](http://stackoverflow.com/users/75170/)

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [July 27, 2011, 10:32pm UTC](https://www.ruby-forum.com/t/is-there-a-safe-stub/209995/2 "2011-07-27T22:32:19Z")

</div>

On Jul 27, 2011, at 2:32 PM, John F. wrote:

> foo.stub(:bar).and\_return(‘baz’)
> 
> What I’d like is something like this:
> 
> # raise ArgumentError unless foo.respond\_to? :bar
> 
> # otherwise, behaves as the regular #stub does
> 
> foo.stub!(:bar).and\_return(‘baz’)
> 
> Thoughts?

This has come up many times before. Please check out

> **[\[rspec-users\] respond\_to? check in rspec-mocks](https://groups.google.com/g/rspec/c/w0TImMJ4zSs)**

and [Undefined method stub report · Issue #15 · rspec/rspec-mocks · GitHub](https://github.com/rspec/rspec-mocks/issues/15) to see where the  
conversation is at this point.

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [July 28, 2011, 1:22am UTC](https://www.ruby-forum.com/t/is-there-a-safe-stub/209995/3 "2011-07-28T01:22:09Z")

</div>

Apologies for not noticing this – I did search, but I wasn’t sure  
exactly how to phrase it. Thanks for the info, David.

## ~ jf

John F.  
Principal Consultant, BitsBuilder  
LI: [http://www.linkedin.com/in/johnxf](http://www.linkedin.com/in/johnxf)  
SO: [http://stackoverflow.com/users/75170/](http://stackoverflow.com/users/75170/)

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [July 28, 2011, 2:32am UTC](https://www.ruby-forum.com/t/is-there-a-safe-stub/209995/4 "2011-07-28T02:32:10Z")

</div>

On Jul 27, 2011, at 5:36 PM, John F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Apologies for not noticing this – I did search, but I wasn’t sure  
> exactly how to phrase it. Thanks for the info, David.

No apology necessary. Just saving myself a bunch of re-typing 🙂 Feel  
free to weigh in on those threads.

Cheers,  
David
