[Mocha] expects.at_least_once VS stubs

F

Fernando Perez

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?
 
A

Alex Young

Fernando said:
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.
 
D

David Masover

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top