views:

1298

answers:

1

In my Rails controller, I'm creating multiple instances of the same model class. I want to add some RSpec expectations so I can test that it is creating the correct number with the correct parameters. So, here's what I have in my spec:

Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => @user.id, :position_id => 1, :is_leader => true)
Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => "2222", :position_id => 2)
Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => "3333", :position_id => 3)
Bandmate.should_receive(:create).with(:band_id => @band.id, :user_id => "4444", :position_id => 4)

This is causing problems because it seems that the Bandmate class can only have 1 "should_receive" expectation set on it. So, when I run the example, I get the following error:

Spec::Mocks::MockExpectationError in 'BandsController should create all the bandmates when created'
Mock 'Class' expected :create with ({:band_id=>1014, :user_id=>999, :position_id=>1, :is_leader=>true}) but received it with ({:band_id=>1014, :user_id=>"2222", :position_id=>"2"})

Those are the correct parameters for the second call to create, but RSpec is testing against the wrong parameters.

Does anyone know how I can set up my should_receive expectations to allow multiple different calls?

+2  A: 

Multiple expectations are not a problem at all. What you're running into are ordering problems, given your specific args on unordered expectations. Check this page for details on ordering expectations.

The short story is that you should add .ordered to the end of each of your expectations.

James Baker
Huh, it's working now even without .ordered I guess I had a mistake in my code and blamed it on RSpec.Thanks for the tip!
Micah