Ryan,<div>Not exactly sure what is the problem your are having ... however&nbsp;</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&nbsp;</div><div><br class="webkit-block-placeholder"></div><div>class CurrentPerson &lt; Activerecord::Base #my assumption</div>
<div><br class="webkit-block-placeholder"></div><div>&nbsp;&nbsp;def find_thing(id)</div><div>&nbsp;&nbsp; &nbsp;self.things.find(id)</div><div>&nbsp;&nbsp;end</div><div><br class="webkit-block-placeholder"></div><div>end</div><div><br class="webkit-block-placeholder">
</div><div>then in controller spec&nbsp;</div><div><br class="webkit-block-placeholder"></div><div>before do<br>&nbsp;&nbsp;@request.host = &quot;subdomain.test.host&quot;<br>&nbsp;&nbsp;@current_person = mock(&quot;person&quot;) &nbsp; &nbsp; &nbsp;# &lt;= THE MOCKS IN QUESTION
<br>&nbsp;&nbsp;@current_person.stub!(:things).and_return( [] )</div><div>&nbsp;&nbsp;@thing = mock_model(Thing)<br>&nbsp;&nbsp;@current_person.stub!(:find_thing).and_return( @thing )<br>end<br><br class="webkit-block-placeholder"></div><div>it &quot;should find the donation requested&quot; do
<br>&nbsp;&nbsp;@current_person.should_receive(:find_thing).once.with(&quot;1&quot;).and_return(@thing)<br>&nbsp;&nbsp;do_get<br>end</div><div><br># things_controller.rb<br>def index ...&nbsp;end<br><br>def show<br>&nbsp;&nbsp;@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> &lt;<a href="mailto:lists@artofmission.com">lists@artofmission.com</a>&gt; 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&#39;s mocking and
<br>stubbing? Basically, I need to test that @current_person.things is<br>getting the find message and returning @thing. I&#39;ve tried stubbing<br>and mocking @current_person.things a number of different ways with no<br>
luck. Here&#39;s my latest attempt:<br><br><br># things_controller_spec.rb<br>before do<br>&nbsp;&nbsp; @request.host = &quot;subdomain.test.host&quot;<br>&nbsp;&nbsp; @thing = mock_model(Thing)<br>&nbsp;&nbsp; @current_person = mock(&quot;person&quot;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# &lt;= THE MOCKS IN QUESTION
<br>&nbsp;&nbsp; @current_person.stub!(:things).and_return(Thing)<br>&nbsp;&nbsp; @current_person.things.stub!(:find).and_return(@thing)<br>end<br><br>def do_get<br>&nbsp;&nbsp; get :show, :id =&gt; &quot;1&quot;<br>end<br><br>it &quot;should find the donation requested&quot; do
<br>&nbsp;&nbsp; # Thing.should_receive(:find).with(&quot;1&quot;).and_return(@thing)<br>&nbsp;&nbsp; @current_person.things.should_receive(:find).with(&quot;1&quot;).and_return<br>(@thing)<br>&nbsp;&nbsp; do_get<br>end<br><br># things_controller.rb<br>
def index<br>&nbsp;&nbsp; @things = @current_person.things<br>end<br><br>def show<br>&nbsp;&nbsp; @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>