Ryan,<div>Not exactly sure what is the problem your are having ... however </div><div><br><div>Is the @current_person being assigned (set up) correctly in the things_controller ?</div><div><br><div>Also may be refactor a #find_thing(...) instance method into the @current_person class to
</div><div>help with the encapsulation of things - literally.</div><div><br class="webkit-block-placeholder"></div><div>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.
</div><div><br class="webkit-block-placeholder"></div><div>For example in the @current_person class </div><div><br class="webkit-block-placeholder"></div><div>class CurrentPerson < Activerecord::Base #my assumption</div>
<div><br class="webkit-block-placeholder"></div><div> def find_thing(id)</div><div> self.things.find(id)</div><div> end</div><div><br class="webkit-block-placeholder"></div><div>end</div><div><br class="webkit-block-placeholder">
</div><div>then in controller spec </div><div><br class="webkit-block-placeholder"></div><div>before do<br> @request.host = "subdomain.test.host"<br> @current_person = mock("person") # <= THE MOCKS IN QUESTION
<br> @current_person.stub!(:things).and_return( [] )</div><div> @thing = mock_model(Thing)<br> @current_person.stub!(:find_thing).and_return( @thing )<br>end<br><br class="webkit-block-placeholder"></div><div>it "should find the donation requested" do
<br> @current_person.should_receive(:find_thing).once.with("1").and_return(@thing)<br> do_get<br>end</div><div><br># things_controller.rb<br>def index ... end<br><br>def show<br> @thing = @current_person.find_thing( params[:id] )
</div><div>end</div><div><br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div><br>
<div><span class="gmail_quote">On 10/25/07, <b class="gmail_sendername">Ryan Heneise</b> <<a href="mailto:lists@artofmission.com">lists@artofmission.com</a>> wrote:</span><blockquote class="gmail_quote" style="margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex">
My app uses account_location to set up subdomains as account keys. In<br>my controllers, all my model operations are scoped through the<br>@current_person object.<br><br>The question is: How do I test this with RSpec's mocking and
<br>stubbing? Basically, I need to test that @current_person.things is<br>getting the find message and returning @thing. I've tried stubbing<br>and mocking @current_person.things a number of different ways with no<br>
luck. Here's my latest attempt:<br><br><br># things_controller_spec.rb<br>before do<br> @request.host = "subdomain.test.host"<br> @thing = mock_model(Thing)<br> @current_person = mock("person") # <= THE MOCKS IN QUESTION
<br> @current_person.stub!(:things).and_return(Thing)<br> @current_person.things.stub!(:find).and_return(@thing)<br>end<br><br>def do_get<br> get :show, :id => "1"<br>end<br><br>it "should find the donation requested" do
<br> # Thing.should_receive(:find).with("1").and_return(@thing)<br> @current_person.things.should_receive(:find).with("1").and_return<br>(@thing)<br> do_get<br>end<br><br># things_controller.rb<br>
def index<br> @things = @current_person.things<br>end<br><br>def show<br> @thing = @current_person.things.find(params[:id])<br>end<br><br><br><br>Thanks for your help.<br><br>P.S. Sorry if this is answered elsewhere - I did a lot of googling
<br>and finally decided to ask the group!<br><br>_______________________________________________<br>rspec-users mailing list<br><a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br><a href="http://rubyforge.org/mailman/listinfo/rspec-users">
http://rubyforge.org/mailman/listinfo/rspec-users</a><br></blockquote></div><br></div></div></div>