Topic: Mocha & make_resourceful errors

I've just started using Mocha for stubbing some methods in my Test::Unit testing.  I also use a plugin called make_resourceful which helps keep my controller code to a minimum.

I can use mocha successfully when I don't use make_resourceful

In my test:
  test "should show car" do
    Car.expects(:find).with("123").returns(@car)

    #    get :show, :id => cars(:one).to_param
    get :show, :id => 123
    assert_response :success
  end


In my controller
def show
  @car = Car.find(params[:id])
end

That works fine.

If I use the make_resourceful command without any overrides then it also works:

make_resourceful do
  actions :all
end

The problem starts to happen when I override some of the behaviour such as:

make_resourceful do
  actions :all

  before :show do 
  @car = Car.find(params[:id])
  end
end

This returns an error 404 when testing but if I use the application it works and if I test using fixtures and no stubbing it works as well.  If I put a debugger line there and test that line I get the error:

Mocha::ExpectationError Exception: unexpected invocation: Car(id: integer, make: string, model: string, year: date, zero_one_hundred: float, four_hundred_mtrs: float, created_at: datetime, updated_at: datetime, user_id: integer).find('123')
satisfied expectations:
- expected exactly once, already invoked once: Car(id: integer, make: string, model: string, year: date, zero_one_hundred: float, four_hundred_mtrs: float, created_at: datetime, updated_at: datetime, user_id: integer).find('123')


Does this mean that make_resourceful is invoking the command '@car = Car.find(params[:id])' as well?  How do I allow that command to be called more than once? 

I actually want to use the command 'current_user.cars.find(params[:id])' in the end, but have been trying to keep my example simple.

I notice if I declare the mocha line twice it works

    Car.expects(:find).with("123").returns(@car)
    Car.expects(:find).with("123").returns(@car)

I shouldn't have to do this though.

Last edited by map7 (2010-03-23 17:46:15)