views:

396

answers:

2

I would like write RSpec for my controller using RR.

I wrote following code:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe RegistrationController do

    it "should work" do 
        #deploy and approve are member functions
        stub.instance_of(Registration).approve { true }
        stub.instance_of(Registration).deploy { true }
        post :register
    end 
end

However RR stubs only deploy method when still calls original approve method.

What syntax should I use to stub both method calls for all instances of Registration class?

UPDATE: I achivied desired result with [Mocha]

Registration.any_instance.stubs(:deploy).returns(true)
Registration.any_instance.stubs(:approve).returns(true)
A: 

as far as I know, the RSpec mocks don't allow you to do that. Are you sure, that you need to stub all instances? I usually follow this pattern:

describe RegistrationController do
    before(:each) do
       @registration = mock_model(Registration, :approve => true, :deploy => true)
       Registration.stub!(:find => @registration)
       # now each call to Registration.find will return my mocked object
    end

    it "should work" do 
      post :register
      reponse.should be_success
    end

    it "should call approve" do
      @registration.should_receive(:approve).once.and_return(true)
      post :register
    end

    # etc 
end

By stubbing the find method of the Registration class you control, what object gets returned in the spec.

jcfischer
thank you it was helpful for me
reuvenab
The question is about RR stubs, not RSpec stubs.
Peeja
ahh - I missed that - should learn to read better
jcfischer
+1  A: 

It would appear the behavior you describe is actually a bug:

http://github.com/btakita/rr/issues#issue/17

jtanium