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

sinclair bain rspec.user at gmail.com
Thu Oct 25 11:52:28 EDT 2007


Ryan,Not exactly sure what is the problem your are having ... however

Is the @current_person being assigned (set up) correctly in the
things_controller ?

Also may be refactor a #find_thing(...) instance method into the
@current_person class to
help with the encapsulation of things - literally.

This pushes the emphasis into the model and its spec for that functioning as
intended then the controller need only verify that it is being called.

For example in the @current_person class

class CurrentPerson < Activerecord::Base #my assumption

  def find_thing(id)
    self.things.find(id)
  end

end

then in controller spec

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

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

# things_controller.rb
def index ... end

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





On 10/25/07, Ryan Heneise <lists at artofmission.com> wrote:
>
> 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!
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071025/e0877b70/attachment.html 


More information about the rspec-users mailing list