0

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)
0

2 Answers 2

1

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

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

-1

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.

1
  • 1
    The question is about RR stubs, not RSpec stubs.
    – Peeja
    Oct 9, 2009 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.