# \[Mocha\] expects.at\_least\_once VS stubs

**URL:** <https://www.ruby-forum.com/t/mocha-expects-at-least-once-vs-stubs/165883>\
**Category:** Ruby\
**Created:** [April 18, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/mocha-expects-at-least-once-vs-stubs/165883 "2009-04-18T21:57:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 18, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/mocha-expects-at-least-once-vs-stubs/165883/1 "2009-04-18T21:57:46Z")

</div>

Hi,

In a Rails app, I often find myself having to write  
expects(:whatever).at\_least\_once.returns(“some\_value”), so it clutters  
my test and I simply ditch expects() in favor of stubs() which won’t  
yell at me because some method was called more than once.

What do you usually use?

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 20, 2009, 3:52pm UTC](https://www.ruby-forum.com/t/mocha-expects-at-least-once-vs-stubs/165883/2 "2009-04-20T15:52:24Z")

</div>

Fernando P. wrote:

> Hi,
> 
> In a Rails app, I often find myself having to write  
> expects(:whatever).at\_least\_once.returns(“some\_value”), so it clutters  
> my test and I simply ditch expects() in favor of stubs() which won’t  
> yell at me because some method was called more than once.
> 
> What do you usually use?

It varies from test to test depending on whether a failure actually  
matters from the point of view of that test. If I am checking, in the  
current test, that a method actually gets called, such that it’s a  
failure if it doesn’t, then I use #expects. If I just need to fake a  
response to get down a certain code path to test something later, I use  
#stubs. Errors masked by using #stubs should get caught by other tests  
where those calls are mocked with #expects.

I find I don’t use #at\_least\_once very much. Either the method gets  
called once, or it gets called in a sequence that I control and should  
be checking for explicitly with #in\_sequence.

–  
Alex

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [April 23, 2009, 12:17am UTC](https://www.ruby-forum.com/t/mocha-expects-at-least-once-vs-stubs/165883/3 "2009-04-23T00:17:26Z")

</div>

On Saturday 18 April 2009 14:57:48 Fernando P. wrote:

> Hi,
> 
> In a Rails app, I often find myself having to write  
> expects(:whatever).at\_least\_once.returns(“some\_value”), so it clutters  
> my test and I simply ditch expects() in favor of stubs() which won’t  
> yell at me because some method was called more than once.

I tend to prefer at\_least\_once if that’s what I want, because then it  
will  
fail if the method isn’t called at all – stubs doesn’t care how many  
times  
the method is called, or even if it’s ever called.
