Forum: Ruby [Mocha] expects.at_least_once VS stubs

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2009-04-18 21:57
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?
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2009-04-20 15:52
Fernando Perez 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
1bc63d01bd3fcccc36fb030a62039352?d=identicon&s=25 David Masover (Guest)
on 2009-04-23 00:17
(Received via mailing list)
On Saturday 18 April 2009 14:57:48 Fernando Perez 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.
This topic is locked and can not be replied to.