Slides from "Uses & Abuses of Mocks & Stubs" at NWRUG

Hi all

I gave a presentation on mock objects this week at the North West Ruby
User Group in Manchester[1]. I thought some of you may be interested
in the slides, which are now on my blog[2].

Note that most of the slides were prepared late at night under sleep
deprivation, so there are a few bits where I had to clear up what I
meant in response to questions. (Mainly, by using examples that are
so trivial it’s not clear what the benefit of a mock object is.) But
hopefully there’s enough value in them that non-attendees might find
them useful.

Feedback gratefully received.

Cheers
Ashley

[1] NWRUG Event: Uses and Abuses of Mocks and Stubs, Thursday 15th October
[2]
http://aviewfromafar.net/2009/10/17/nwrug-slides-uses-and-abuses-of-mocks-and-stubs


http://www.patchspace.co.uk/
http://www.linkedin.com/in/ashleymoran

Response written as I go through the slides, and late at night under
sleep deprivation :wink:

  • 57 juicy slides in half an hour? Damn dude.
  • Not a fan of the “defining an interface” service example. A single
    method named #query that accepts a string that appears to have the
    real command is not much of an interface. What about something like
    service.should_receive(:store).with(:first_name => ‘Fred’, :last_name
    => ‘Flintstone’) ?
  • I dig the be_equivalent_xml_to matcher
  • On the Simulating Situations slide I would stub the call to #save
    instead of expecting it. You’re just setting up context for the
    example to run in.
  • may be a style thing but in your Degrees of Freedom shapes examples
    I don’t like creating the shapes in a before. Just inline it
  • the shape example where you pass in a mock is really weird to me.
    What are you trying to illustrate? A different example might be more
    effective (or I just need to hear the commentary that goes with that
    slide)

Over all it looks like a great introduction to the benefits, use, and
pitfalls of mocks. Thanks for sharing.

Pat

On Sat, Oct 17, 2009 at 12:54 PM, Ashley M.

On 21 Oct 2009, at 09:46, Pat M. wrote:

Response written as I go through the slides, and late at night under
sleep deprivation :wink:

Seems fitting, I wrote them under the exact same conditions =)

  • 57 juicy slides in half an hour? Damn dude.

Well, might have had about 45-50 mins, but there were plenty of
questions in the middle. Didn’t have time to prune the slides down
before the presentation. I was ready to collapse at the end :slight_smile:

Would have had longer, but Boxedup.com sponsored free pizza at the bar
opposite. So literally before I’d shut my laptop lid, there was a
mass stampede out of the building…

  • Not a fan of the “defining an interface” service example. A single
    method named #query that accepts a string that appears to have the
    real command is not much of an interface. What about something like
    service.should_receive(:store).with(:first_name => ‘Fred’, :last_name
    => ‘Flintstone’) ?

I think you’re right. Your example is better.

  • I dig the be_equivalent_xml_to matcher

Our implementation is a bit simplistic, but it’s still useful. I’ve
put it up as a gist for it[1] in case anyone else finds it useful.

  • On the Simulating Situations slide I would stub the call to #save
    instead of expecting it. You’re just setting up context for the
    example to run in.

Ah, I just didn’t know off the top of my head how to make a stub
method raise an error. You’re right though, it would be much better
if I had.

  • may be a style thing but in your Degrees of Freedom shapes examples
    I don’t like creating the shapes in a before. Just inline it

It’s funny you say that, when I wrote it I was thinking it was not
necessary to use a before block, but “that’s the style I like”. I
almost always separate out the SUT from the examples. I think it goes
back to the days when I thought in rigid Given-When-Then terms.

  • the shape example where you pass in a mock is really weird to me.
    What are you trying to illustrate? A different example might be more
    effective (or I just need to hear the commentary that goes with that
    slide)

Someone else raised an issue with the same slide, so it must really
suck. The fact the example is written around a getter should have
raised an exception in my head (NotEnoughSleepError). A much better
example is slide 30 (Hiding Random Behaviour) where @first and @values
are used in the same manner (to reduce the need to triangulate to
prove the implementation is not degenerate).

Over all it looks like a great introduction to the benefits, use, and
pitfalls of mocks. Thanks for sharing.

Thanks for the feedback! It’s the most detailed I’ve had so far. If
I repeat the presentation I’ll make all the changes you suggested.
Except for the liberal use of before blocks :slight_smile:

Also I should probably add that at the end, when I was reading the
references, one of the comments I made was along the lines of “I
finally got round to reading the RSpec Book chapter on mocks the other
day, and I was really disappointed. It said everything I was trying
to, really clearly, and I was tempted to just turn up and read it out
loud. So go look at that next.” I daren’t read the rest of the new
chapters in case it makes the rest of my job redundant =)

Cheers
Ashley

[1] equivalent_xml_matcher.rb · GitHub


http://www.patchspace.co.uk/
http://www.linkedin.com/in/ashleymoran