That's great!<br><div><br class="webkit-block-placeholder"></div><div>Cheers!</div><div>sinclair<br><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">On Oct 26, 2007, at 12:11 PM, sinclair bain wrote:<br><br>> Ryan,
<br>><br>> Are you stubbing<br>><br>> @current_company = Company.find_by_subdomain(account_subdomain)<br>><br>> in the inherited #find_current_company method ?<br>><br>> Make sure the @current_company is the same one upon which you have
<br>> your expectations ?<br>> ie<br>><br>> before do<br>> ...<br>> Company.stub!(:find_by_subdomain).and_return(@current_company)<br>><br>><br>> end<br>><br>> A pint and a bag of crisps says that does it ! ; )
<br>><br>> Cheers!<br>> sinclair<br><br><br>Absolutely smashing! It worked! Here's my setup now:<br><br>before do<br> @item = mock_model(Item)<br> @current_company = mock_model(Company)<br> Company.stub!
(:find_by_subdomain).and_return(@current_company)<br> @current_company.stub!(:find_items).with("1").and_return(@item)<br>end<br><br>What's your address - I'll send you a pint and a bag of crisps :)<br>
<br><br>Ryan<br><br><br><br><br><br><br><br><br>> On 10/26/07, Ryan Heneise < <a href="mailto:lists@artofmission.com">lists@artofmission.com</a>> wrote:How do<br>> 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>><br>><br>><br>> --<br>><br>><br>> Cheers!<br>> sinclair<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><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>