[rspec-devel] [ rspec-Feature Requests-9083 ] Allow ordering of expectations across mocks

noreply at rubyforge.org noreply at rubyforge.org
Mon Mar 12 12:55:46 EDT 2007


Feature Requests item #9083, was opened at 2007-03-06 09:21
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=9083&group_id=797

Category: mock module
Group: None
Status: Open
Priority: 3
Submitted By: Ashley Moran (ashley_moran)
Assigned to: David Chelimsky (dchelimsky)
Summary: Allow ordering of expectations across mocks

Initial Comment:
I'd like to be able to write something like this:

context "Two mocks"
  setup do
    @mock_1 = mock("1")
    @mock_1.stub!(:moo)
    @mock_2 = mock("2")
    @mock_2.stub!(:quack)
  end

  specify "should receive their messages in a certain order"
    @mock_2.should_receive(:quack).after(@mock_1.receives(:moo))
  end
end

or something like that...


----------------------------------------------------------------------

>Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2007-03-12 12:55

Message:
Ironically I just found myself needing what you're asking for, so I guess it can't be abuse then ;-) (just like 3 of the JMock authors I am an ex-ThoughtWorker, and there was never an official TW opinion on mocking)


Anyway, It's easily achievable with a block (mocks take blocks in should_receive). Example:

foo.should_receive(:bar) do
  ping.should_receive(:pong)
end

This will only pass with foo.bar;ping.pong, and will fail with the opposite order.

Can you use this technique?


----------------------------------------------------------------------

Comment By: Ashley Moran (ashley_moran)
Date: 2007-03-07 03:24

Message:
Hmm seems like I've been shot down on this!!!

I'm just searching for a way to spec that my database statements run in a 
transaction.  There was a suggestion posted on rspec-users that would involve 
monitoring the order of statments.

> This is bad use of mocking

Oh no, it can't be! ;) http://www.jmock.org/oopsla2004.pdf (section 3.4)

Maybe the ThoughtWorks team have revised their opinion on mock method 
ordering since 2004... but in that paper they present it as a valid technique.  
To my sleep-deprived eyes, I can't tell whether it looks suspect or not.

----------------------------------------------------------------------

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2007-03-06 11:03

Message:
I agree with David. This is bad use of mocking. It will lead to too brittle specs.

----------------------------------------------------------------------

Comment By: David Chelimsky (dchelimsky)
Date: 2007-03-06 09:58

Message:
This sounds like mock abuse to me and I'm inclined not to support it in rspec. Can you give me a sense of the value you're looking for from this? A specific example maybe?

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=9083&group_id=797


More information about the rspec-devel mailing list