[Mocha] expects.at_least_once VS stubs


#1

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?


#2

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


#3

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.