[rspec-users] Mocking/Stubbing help with subdomain as account key

Ryan Heneise lists at artofmission.com
Thu Oct 25 10:23:52 EDT 2007


My app uses account_location to set up subdomains as account keys. In  
my controllers, all my model operations are scoped through the  
@current_person object.

The question is: How do I test this with RSpec's mocking and  
stubbing? Basically, I need to test that @current_person.things is  
getting the find message and returning @thing. I've tried stubbing  
and mocking @current_person.things a number of different ways with no  
luck. Here's my latest attempt:


# things_controller_spec.rb
before do
   @request.host = "subdomain.test.host"
   @thing = mock_model(Thing)
   @current_person = mock("person")      # <= THE MOCKS IN QUESTION
   @current_person.stub!(:things).and_return(Thing)
   @current_person.things.stub!(:find).and_return(@thing)
end

def do_get
   get :show, :id => "1"
end

it "should find the donation requested" do
   # Thing.should_receive(:find).with("1").and_return(@thing)
   @current_person.things.should_receive(:find).with("1").and_return 
(@thing)
   do_get
end

# things_controller.rb
def index
   @things = @current_person.things
end

def show
   @thing = @current_person.things.find(params[:id])
end



Thanks for your help.

P.S. Sorry if this is answered elsewhere - I did a lot of googling  
and finally decided to ask the group!



More information about the rspec-users mailing list