[rspec-users] Expecting arbitrary method calls in a particular order

Pat Maddox pergesu at gmail.com
Sun Jul 13 10:32:58 EDT 2008


On Sun, Jul 13, 2008 at 9:49 AM, Sven Fuchs <svenfuchs at artweb-design.de> wrote:
> I've been wondering how to expect arbitrary methods being called in a
> particular order. The RSpec documentation for expecting method calls on mock
> objects mentions that it is possible to pass a block to #should_receive, but
> does not mention that it can be used to track the method call order pretty
> easily:
>
> http://www.artweb-design.de/2008/7/13/expecting-arbitrary-method-calls-in-a-particular-order-in-rspec
>
> Is there a better way of doing this?
>
> If not, do you think it would make sense to add this to the RSpec docs?
> http://rspec.info/documentation/mocks/message_expectations.html

Hey Sven,

Mock objects have an optional notion of ordering.  Check out the following spec:

describe "a mock object" do
  it "should expect ordered messages" do
    foo = mock("ordered mock")
    foo.should_receive(:first).ordered
    foo.should_receive(:second).ordered
    foo.should_receive(:third).ordered

    foo.first
    foo.second
    foo.third
  end
end

Changing the order in which they're called will raise an ExpectationNotMetError.

Pat


More information about the rspec-users mailing list