Ryan,<div><br class="webkit-block-placeholder"></div><div>Are you stubbing </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="'Courier New'" 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;"> in the inherited #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> ...</div><div> Company.stub!(:find_by_subdomain).and_return(@current_company)<br><br> </div><div>end</div><div><br class="webkit-block-placeholder"></div><div>A pint and a bag of crisps says that does it ! ; )
</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> <<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">How do you go about implementing and rspecing subdomains as account
<br>keys? I'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's Revenge (<a href="http://www.lukeredpath.co.uk/">http://www.lukeredpath.co.uk/</a><br>2007/10/18/demeters-revenge), and reading Sinclair'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 'Company' 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'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 "Mocking/Stubbing help<br>with subdomain as account key"<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'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, "handling GET /items" do<br>
fixtures :companies<br><br> before do<br> @request.host = "subdomain.test.host"<br> @item = mock(Item)<br> Item.stub!(:find).and_return([@item])<br> @current_company = mock(Company)<br> @current_company.stub!(:items).and_return([])
<br> @current_company.stub!(:find_items).and_return([@item])<br> @current_company.stub!(:subdomain).and_return("subdomain")<br> end<br><br> # Passes<br> def do_get<br> get :index<br> end<br>
<br> # Passes<br> it "should be successful" do<br> do_get<br> response.should be_success<br> end<br><br> # Passes<br> it "should render index template" do<br> do_get<br> response.should
render_template('index')<br> end<br><br> # FAILS with message:<br> # Mock 'Company' expected :find_items with (:all) once, but<br>received it 0 times<br> it "should find all items" do<br>
@current_company.should_receive(:find_items).with<br>(:all).and_return([@item])<br> do_get<br> end<br><br> # Passes<br> it "should assign the found items for the view" do<br> do_get<br> assigns[:items].should == [@item]
<br> end<br>end<br><br><br># items_controller.rb<br>class ItemsController < ApplicationController<br> def index<br> @items = @current_company.find_items(:all)<br> end<br>end<br><br><br># company.rb<br>class Company < ActiveRecord::Base
<br> has_many :items<br> def find_items(*args)<br> items.find(*args)<br> end<br>end<br><br><br># application.rb<br>class ApplicationController < ActionController::Base<br> before_filter :find_current_company
<br> def find_current_company<br> @current_company = Company.find_by_subdomain(account_subdomain)<br> 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>