Ryan,<div><br class="webkit-block-placeholder"></div><div>Are you stubbing&nbsp;</div><div style="text-align: justify;"><br class="webkit-block-placeholder"></div><div style="text-align: justify;"><font class="Apple-style-span" color="#110000" face="&#39;Courier New&#39;" size="3">
<span class="Apple-style-span" style="border-collapse: collapse; font-size: 11px; line-height: 16px; white-space: pre; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: arial; font-size: 13px; line-height: normal; white-space: normal; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; ">
@current_company = <b><span class="Apple-style-span" style="text-decoration: underline; -webkit-text-decorations-in-effect: underline; "><font class="Apple-style-span" size="1"><span class="Apple-style-span" style="font-size: 13px; ">
Company.find_by_subdomain(account_subdomain)</span></font></span></b></span></span></font></div><div style="text-align: justify;"><br class="webkit-block-placeholder"></div><div style="text-align: justify;">&nbsp;in the inherited &nbsp;#find_current_company method ?
</div><div><br class="webkit-block-placeholder"></div><div>Make sure the @current_company is the same one upon which you have your expectations ?<br><div>ie</div><div><br class="webkit-block-placeholder"></div><div>before do
</div><div>&nbsp;...</div><div>&nbsp;Company.stub!(:find_by_subdomain).and_return(@current_company)<br><br>&nbsp;</div><div>end</div><div><br class="webkit-block-placeholder"></div><div>A pint and a bag of crisps says that does it ! &nbsp;; )
</div><div><br class="webkit-block-placeholder"></div><div>Cheers!</div><div>sinclair</div><div><br><div><span class="gmail_quote">On 10/26/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">How do you go about implementing and rspecing subdomains as account
<br>keys? I&#39;m sure that this must be an issues for others as well.<br><br>So I have an app using subdomains as account keys. The Application<br>Controller sets up @current_company in a before filter. Everything is<br>
done within the context of the @current_company.<br><br>After reading about Demeter&#39;s Revenge (<a href="http://www.lukeredpath.co.uk/">http://www.lukeredpath.co.uk/</a><br>2007/10/18/demeters-revenge), and reading Sinclair&#39;s advice (link
<br>above), I refactored @current_company.items.find(:all) to<br>@current_company.find_items.<br><br>Still no luck. The spec fails with:<br>-- Mock &#39;Company&#39; expected :find_items with (:all) once, but received<br>
it 0 times<br><br>The other thing that bugs me is the size of the setup method. It&#39;s<br>getting too long and starting to smell. Obviously I must be making<br>this harder on myself that it needs to be.<br><br>Any ideas?
<br><br>(This question was originally posted under &quot;Mocking/Stubbing help<br>with subdomain as account key&quot;<br>- <a href="http://rubyforge.org/pipermail/rspec-users/2007-October/">http://rubyforge.org/pipermail/rspec-users/2007-October/
</a><br>004138.html, but I&#39;ve rephrased it to make it more clear with the<br>subdomain as account key question.)<br><br><br># items_controller_spec.rb<br>describe ItemsController, &quot;handling GET /items&quot; do<br>
&nbsp;&nbsp; fixtures :companies<br><br>&nbsp;&nbsp; before do<br>&nbsp;&nbsp;&nbsp;&nbsp; @request.host = &quot;subdomain.test.host&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp; @item = mock(Item)<br>&nbsp;&nbsp;&nbsp;&nbsp; Item.stub!(:find).and_return([@item])<br>&nbsp;&nbsp;&nbsp;&nbsp; @current_company = mock(Company)<br>&nbsp;&nbsp;&nbsp;&nbsp; @current_company.stub!(:items).and_return([])
<br>&nbsp;&nbsp;&nbsp;&nbsp; @current_company.stub!(:find_items).and_return([@item])<br>&nbsp;&nbsp;&nbsp;&nbsp; @current_company.stub!(:subdomain).and_return(&quot;subdomain&quot;)<br>&nbsp;&nbsp; end<br><br>&nbsp;&nbsp;&nbsp;&nbsp;# Passes<br>&nbsp;&nbsp; def do_get<br>&nbsp;&nbsp;&nbsp;&nbsp; get :index<br>&nbsp;&nbsp; end<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;# Passes<br>&nbsp;&nbsp; it &quot;should be successful&quot; do<br>&nbsp;&nbsp;&nbsp;&nbsp; do_get<br>&nbsp;&nbsp;&nbsp;&nbsp; response.should be_success<br>&nbsp;&nbsp; end<br><br>&nbsp;&nbsp;&nbsp;&nbsp;# Passes<br>&nbsp;&nbsp; it &quot;should render index template&quot; do<br>&nbsp;&nbsp;&nbsp;&nbsp; do_get<br>&nbsp;&nbsp;&nbsp;&nbsp; response.should
 render_template(&#39;index&#39;)<br>&nbsp;&nbsp; end<br><br>&nbsp;&nbsp; # FAILS with message:<br>&nbsp;&nbsp; # Mock &#39;Company&#39; expected :find_items with (:all) once, but<br>received it 0 times<br>&nbsp;&nbsp; it &quot;should find all items&quot; do<br>
&nbsp;&nbsp;&nbsp;&nbsp; @current_company.should_receive(:find_items).with<br>(:all).and_return([@item])<br>&nbsp;&nbsp;&nbsp;&nbsp; do_get<br>&nbsp;&nbsp; end<br><br>&nbsp;&nbsp; # Passes<br>&nbsp;&nbsp; it &quot;should assign the found items for the view&quot; do<br>&nbsp;&nbsp;&nbsp;&nbsp; do_get<br>&nbsp;&nbsp;&nbsp;&nbsp; assigns[:items].should == [@item]
<br>&nbsp;&nbsp; end<br>end<br><br><br># items_controller.rb<br>class ItemsController &lt; ApplicationController<br>&nbsp;&nbsp; def index<br>&nbsp;&nbsp;&nbsp;&nbsp; @items = @current_company.find_items(:all)<br>&nbsp;&nbsp; end<br>end<br><br><br># company.rb<br>class Company &lt; ActiveRecord::Base
<br>&nbsp;&nbsp; has_many :items<br>&nbsp;&nbsp; def find_items(*args)<br>&nbsp;&nbsp;&nbsp;&nbsp; items.find(*args)<br>&nbsp;&nbsp; end<br>end<br><br><br># application.rb<br>class ApplicationController &lt; ActionController::Base<br>&nbsp;&nbsp; before_filter :find_current_company
<br>&nbsp;&nbsp; def find_current_company<br>&nbsp;&nbsp;&nbsp;&nbsp; @current_company = Company.find_by_subdomain(account_subdomain)<br>&nbsp;&nbsp; end<br>end<br><br><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><br clear="all"><br>-- <br><br><br>Cheers!<br>sinclair
</div></div>