[mocha-developer] Ordered Mocking

James Mead
Thu, 01 May 2008 21:41:09 -0000

2008/5/1 Dustin Tinney <tinney at gmail.com>:

> Is there any way to do ordered mocking with in mocha?  And if not is there
a reason, other than development time, why it isn''t
supported?____________________

Hi Dustin,

Thanks for your interest. Please note that your emails were delayed
because you had not subscribed to the list and they had to be
moderated.

There are a couple of ways of doing ordered mocking - sequences &
states (c.f. JMock v2). These are in trunk, but have not been released
yet, although I think sequences may have slipped into the v0.5.6
release by mistake (albeit undocumented). Here is the rdoc in case you
are interested...

   sequence(name) ? sequence

Returns a new sequence that is used to constrain the order in which
expectations can occur.

See also Expectation#in_sequence.

  drawing = sequence(''drawing'')

  turtle = mock(''turtle'')
  turtle.expects(:forward).with(10).in_sequence(drawing)
  turtle.expects(:turn).with(45).in_sequence(drawing)
  turtle.expects(:forward).with(10).in_sequence(drawing)


states(name) ? state_machine

Returns a new state machine that is used to constrain the order in
which expectations can occur.

See also Expectation#then, Expectation#when and StateMachine#starts_as.

  pen = states(''pen'').starts_as(''up'')

  turtle = mock(''turtle'')
  turtle.expects(:pen_down).then(pen.is(''down''))
  turtle.expects(:forward).with(10).when(pen.is(''down''))
  turtle.expects(:turn).with(90).when(pen.is(''down''))
  turtle.expects(:forward).with(10).when(pen.is(''down''))
  turtle.expects(:pen_up).then(pen.is(''up''))

I hope to provide a new release of Mocha very shortly.

--
James.