[mocha-developer] Returning the mock associated with an expectation.

James Mead jamesmead44 at gmail.com
Sun Nov 4 15:19:17 EST 2007

On 04/11/2007, Duncan Beevers <duncanbeevers at gmail.com> wrote:
> I was reading through the FlexMock docs and noticed the expectation
> method .mock, which returns the original mock associated with an
> expectation.
> It looks really handy for writing nice all-in-one mocks like:
> mock_user = mock('User').expects(:first_name).returns('Jonah').mock
> So I started playing around with mocha and found I could actually
> already do this!
> But I'm not sure how.  There's no attr_reader on @mock, and I couldn't
> find out where this method is defined.
> So, perhaps we could add this an explicit method on Expectation and
> include a little rdoc on it?
> I think a lot of people would get benefit from making this explicit.
> Opinions?

Hmm. I think this may have disappeared in some recent refactoring in trunk.
Rather than exposing too much of the internals, I keep meaning to add a
syntax like this...

  mock_user = mock('User') {

Would that help you do what you want to do...?

Alternatively did you know you could already do...

  mock_user = mock('User', :first_name => 'James', :last_name => 'Mead')

However there are a couple of limitations with this - you can't mix expects
& stubs and you can't specify anything other than a returns() e.g. no
with(), no raises(), etc.

What do you think?

More information about the mocha-developer mailing list